4.1作业
实验任务1
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 5 5 int main() { 6 int number; 7 int i; 8 srand(time(0)); 9 for(i = 0; i < N; ++i) { 10 number = rand() % 100 + 1; 11 printf("20490042%04d\n", number); 12 } 13 return 0; 14 }
运行结果

回答问题
问题1
这个程序的功能是在所给的学号范围内随机抽取五个学号。
问题2
rand()用于生成一个随机整数,%是取模运算,%100得到0~99的余数,从而把随机数结果限制在0~99。+1把范围变为1~100。
故line 13的作用是取一个范围在1~100内的随机整数。
问题3
以十进制输出一个宽度为4个字符的整数,位数不足四位用0填充。
问题4
line 14使得每次代码生成的结果都不一样,去掉这行代码后生成的是固定的5个随机数。(猜测是因为随机种子相同)
实验任务2
代码
#include <stdio.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"); return 0; }
运行结果

回答问题
问题1
结账的时候会加上之前的购买金额一起结。
问题2
continue的作用是跳过当前循环中的剩余语句,直接进入下一个循环的条件判断。
实验任务3
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(){ 5 char lit; 6 7 while (scanf(" %c",&lit)!=EOF){ 8 switch (lit) { 9 case 'r': 10 printf("stop!\n"); 11 break; 12 case 'g': 13 printf("go go go\n"); 14 break; 15 case 'y': 16 printf("wait a minute\n"); 17 break; 18 default: 19 printf("something must be wrong...\n"); 20 break; 21 } 22 } 23 system ("pause"); 24 return 0; 25 }
运行结果

实验任务4
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(){ 5 double expense; 6 double total = 0.0; 7 double max = 0.0; 8 double min = 0.0; 9 int first = 1; 10 11 while (1){ 12 scanf ("%lf",&expense); 13 14 if (expense == -1){ 15 break; 16 } 17 18 if (expense > 0 && expense <= 20000) { 19 total += expense; 20 21 if (first){ 22 max = expense; 23 min = expense; 24 first = 0; 25 }else{ 26 if (expense > max){ 27 max = expense; 28 } 29 if (expense < min){ 30 min = expense; 31 } 32 } 33 } 34 } 35 36 printf ("今日累计消费总额:%.1f\n",total); 37 printf ("今日最高一笔开销:%.1f\n",max); 38 printf ("今日最低一笔开销:%.1f\n",min); 39 40 system ("pause"); 41 return 0; 42 }
运行结果

实验任务5
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(){ 5 int a,b,c; 6 while(scanf("%d %d %d", &a,&b,&c)!=EOF){ 7 if (a+b<=c || a+c<=b || b+c<=a){ 8 printf ("不能构成三角形\n"); 9 } 10 else if (a==b && b==c){ 11 printf("等边三角形"); 12 } 13 else if (a==b || b==c || a==c){ 14 printf("等腰三角形"); 15 } 16 else if (a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a ){ 17 printf("直角三角形"); 18 } 19 else{ 20 printf("普通三角形"); 21 } 22 } 23 system ("pause"); 24 return 0; 25 }
运行结果

实验任务6
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 int luckyday, guess, chance = 3; 7 8 srand(time(0)); 9 luckyday = rand() % 30 + 1; 10 11 printf("猜猜2026年4月哪一天是你的luckyday\n"); 12 13 printf("开始喽,你有3次机会,猜吧(1~30):"); 14 scanf("%d", &guess); 15 chance--; 16 17 while (chance >= 0) { 18 if (guess == luckyday) { 19 printf("哇,猜中了:)\n"); 20 break; 21 } 22 else if (guess < luckyday) { 23 printf("你猜的日期早了,你的luckyday还没到呢\n"); 24 } 25 else { 26 printf("你猜的日期晚了,你的luckyday在前面哦\n"); 27 } 28 29 if (chance == 0) { 30 printf("次数用光啦。4月你的luckyday是%d号\n", luckyday); 31 break; 32 } 33 34 printf("再猜(1~30):"); 35 scanf("%d", &guess); 36 chance--; 37 } 38 39 system ("pause"); 40 return 0; 41 }
运行结果



浙公网安备 33010602011771号