当前位置: 首页 > news >正文

实验任务2 - pp

task-1

源代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define N1 80
#define N2 35
int main() {int cnt;int random_major, random_no;
srand(time(NULL));// 以当前系统时间作为随机种子
cnt = 0;
while(cnt < N) {
random_major = rand() % 2;
if(random_major) {
random_no = rand() % N1 + 1;
printf("20256343%04d\n", random_no);}
else {
random_no = rand() % N2 + 1;
printf("20256136%04d\n", random_no);
}
cnt++;
}
system("pause");
return 0;
}

运行结果截图

捕获

捕获2

删去代码后运行结果截图

1.3

1.4

问题1

产生新的随机数

问题2

随机抽取5个学号

task-2

 源代码

#include <stdio.h>
#include<stdlib.h>
int main() {
int choice, quantity;
float total_price = 0, amount_paid, change;
while (1) {
printf("\n自动饮料售卖机菜单:\n");printf("1. 可乐 - 3 元/瓶\n");
printf("2. 雪碧 - 3 元/瓶\n");
printf("3. 橙汁 - 5 元/瓶\n");
printf("4. 矿泉水 - 2 元/瓶\n");
printf("0. 退出购买流程\n");
printf("请输入饮料编号: ");
scanf("%d", &choice);
if (choice == 0)
break;
if (choice < 1 || choice > 4)
{printf("无效的饮料编号,请重新输入。\n");
continue;
}
printf("请输入购买的数量: ");
scanf("%d", &quantity);
if (quantity < 0) {
printf("购买数量不能为负数,请重新输入。\n");
continue;
}
if(choice == 1 || choice == 2)
total_price += 3 * quantity;
else if(choice == 3)
total_price += 5 * quantity;
else
total_price += 2 * quantity;
printf("请投入金额: ");
scanf("%f", &amount_paid);
change = amount_paid - total_price;
printf("本次购买总价: %.2f 元\n", total_price);printf("找零:%.2f 元\n",change);
total_price = 0;
}printf("感谢您的购买,欢迎下次光临!\n");system("pause");
return 0;
}

运行结果截图

2.1

 

删去line47代码后运行结果截图

22

问题1

输出结果中“本次购买总价”会累加

问题2

结束本次循环,进入下一次循环

task-3

 源代码

#include <stdio.h>
#include<stdlib.h>
int main()
{char a;while (scanf("%c", &a)!=EOF){if (a == 'r'){printf("stop!\n");}else if (a == 'g'){printf("go go go\n");}else if (a == 'y'){printf("wait a minute\n");}else{printf("something must be wrong\n");}getchar();}system("pause");return 0;
}

 

运行结果截图

屏幕截图 2025-10-14 094235

task-4

源代码

#include <stdio.h>
#include<stdlib.h>
int main()
{double a=1,sum=0,b=0,c=20001;printf("输入今日开销,直到输入-1终止:\n");while (a != -1){scanf("%lf", &a);if (a > 0 && a <= 20000){sum += a;if (a > b){b = a;}if (a < c){c = a;}}}printf("今日累计消费总额:%.1f\n", sum);printf("今日最高一笔开销:%.1f\n", b);printf("今日最低一笔开销:%.1f\n", c);return 0;
} 

运行结果截图

屏幕截图 2025-10-14 102607

 task-5

源代码

#include <stdio.h>
#include<stdlib.h>
int main()
{int a, b, c;while (scanf("%d%d%d", &a, &b, &c)!=EOF){if (a + b > c && a + c > b && b + c > a){if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a){printf("直角三角形\n");}else if (a == b && a == c){printf("等边三角形\n");}else if (a == b || a == c || b == c){printf("等腰三角形\n");}else{printf("普通三角形\n");}}elseprintf("不能构成三角形\n");}return 0;
} 

运行结果截图

屏幕截图 2025-10-14 104655

task-6

源代码

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{int n,random,i=0;srand(time(NULL));random = rand() % 30 + 1;printf("猜猜2025年11月哪一天是你的lucky day\n");printf("开始喽,你有三次机会,猜吧(1~30):");while (i < 3){scanf("%d", &n);if (n < random){printf("你猜的日期早了,你的lucky day还没到呢\n");}if (n > random){printf("你猜的日期晚了,你的lucky day在前面哦\n");}if (n== random){printf("哇,猜中了:)\n");break;}i++;if (i <3){printf("再猜(1~30):");}}if (n != random){printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d\n",random);}return 0;
}

运行结果截图

屏幕截图 2025-10-14 112701

屏幕截图 2025-10-14 113142

实验总结

1.习惯使用换行符
2.
要求支持多组输入,直到用户按下CTRL+Z,回车,终止程序时,用
while (scanf("%c", &a)!=EOF)
       {
        }
3.要关注数据范围
找一组数的最小值时,令表示其的变量初始值大于改组数的最大值
4.
已知循环次数最好用for
5.
生成随机数  

# &nbsp;        #include<time.h>

# &nbsp;        确定类型

# &nbsp;        srand(time(NULL));

# &nbsp;        确定随机数范围random = rand() % 30 + 1;(指1~30)

# &nbsp;                random = rand() % 30(指0~29)
http://www.rkmt.cn/news/20765.html

相关文章:

  • 第二次实验作业
  • ControlNet——AI图像生成的“精准操控工具”
  • 2025 武汉实缴服务机构最新推荐排行榜:知识产权 / 注册资本代办优选清单,深度解析专业服务品质
  • 实用指南:消息队列 MQ
  • 2025 年最新推荐高性价比实木家具厂家排行榜:涵盖实木床餐边柜/餐桌斗柜/书柜/梳妆台/床头柜/餐椅沙发/休闲椅优质厂家精选
  • 多智能体微服务实战(3/4):Aspire 打造本地 K8s 开发环境
  • 一文读懂Optimism,Arbitrum,ZK Rollups 共识算法
  • notepad++中使用正则表达式过滤数据
  • 2025 年风机厂家最新推荐排行榜:聚焦交流 / 直流 / 无刷 / 大吸力 / 调速 / 小型高压等多类型风机,精选优质企业助力采购决策
  • 简单高效的SQL注入测试方法:Break Repair技术详解
  • AG Grid推出全新MCP服务器——让AI更智能地理解你的数据表格!
  • uniapp 判断在特定app或h5里还需要判断当前环境
  • Excel处理控件Aspose.Cells教程:使用Python将TXT文件转换为CSV
  • 2025 年干燥机厂商最新推荐排行榜:聚焦实验室与工业用优质设备,精选实力品牌供采购参考工业喷雾 / 陶瓷喷雾 / 制粒 / 奶粉喷雾 / 离心喷雾干燥机厂家推荐
  • CF Round 1024 / CF2101
  • 本地 Git 清理已经在远程删除的分支引用
  • 一切皆有逻辑,元推理框架是逻辑真相生成器
  • 2025 年工业减速机厂家最新推荐排行榜:聚焦谐波 / 行星 / 直角换向器等多类型设备,精选实力企业助力采购决策
  • OpenCV、EmguCV和OpenCvSharp访问图像耗时测评(附源码)
  • Exp2
  • 2025 年流量计厂家最新推荐排行榜:聚焦国内优质厂商,覆盖电磁涡街等多类型产品,助力企业精准选型避开风险液体质量/金属管浮子/液体涡轮/气体涡轮/旋进漩涡/空气流量计厂家推荐
  • 2025石墨聚苯板源头厂家推荐榜,岩棉板/石墨保温板/EPS泡沫板公司推荐
  • 多智能体微服务实战(1/4):康威定律在 AI 时代的应用
  • 2025 年最新雕花板厂家推荐排行榜:聚焦金属 / 外墙 / 保温 / 隔热雕花板,助力采购方精准选优质品牌
  • 2025 年感应门源头厂家最新推荐排行榜:实力品牌甄选与选购指南,助您精准对接优质供应商
  • 2025 年国内自动门厂商最新推荐排行榜:涵盖伸缩 / 感应 / 平开 / 玻璃 / 无框自动门,助您选优质产品
  • 2025 最新推荐!溴化锂回收公司精选排行榜:制冷机 / 溶液 / 机组回收服务商权威测评及选择指南
  • JS队列之双端队列介绍
  • LGP3372 [LG TPLT] 线段树一 学习笔记
  • Day27 | Java集合框架之List接口详解 - 实践