实验2
task1
源代码
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 }
运行结果截图

回答问题
问题一:随机生成五个204900420001-204900420100之间的学号
问题二:生成一个1-100的随机数
问题三:保证20490042后面是四位数
问题四:使每次rand()生成的随机数不同
task2
源代码
1 #include <stdio.h> 2 int main(){ 3 int choice,quantity; 4 float total_price=0,amount_paid,change; 5 while(1){ 6 printf("\n自动饮料售卖机菜单:\n"); 7 printf("1. 可乐-3元/瓶\n"); 8 printf("2. 雪碧-3元/瓶\n"); 9 printf("3. 橙汁-5元/瓶\n"); 10 printf("4. 矿泉水-2元/瓶\n"); 11 printf("0. 退出购买流程\n"); 12 printf("请输入饮料编号: "); 13 scanf("%d",&choice); 14 if(choice==0) 15 break; 16 if(choice<1||choice>4){ 17 printf("无效的饮料编号,请重新输入。\n"); 18 continue; 19 } 20 printf("请输入购买的数量:"); 21 scanf("%d",&quantity); 22 if(quantity<0){ 23 printf("购买数量不能为负数,请重新输入。\n"); 24 continue; 25 } 26 if(choice==1||choice==2) 27 total_price+=3*quantity; 28 else if(choice==3) 29 total_price+=5*quantity; 30 else 31 total_price+=2*quantity; 32 printf("请投入金额:"); 33 scanf("%f",&amount_paid); 34 change=amount_paid-total_price; 35 printf("本次购买总价:%.2f元\n",total_price); 36 printf("找零:%.2f元\n",change); 37 total_price=0; 38 }printf("感谢您的购买,欢迎下次光临!\n"); 39 return 0; 40 }
运行结果截图

回答问题
问题一:去掉line47会导致购买时会把上一次的总价加到新的总价上
问题二:跳过continue之后语句,直接进入下一次循环条件判断
task3
源代码
1 #include <stdio.h> 2 int main(){ 3 char light; 4 while(scanf("%c",&light)==1){ 5 getchar(); 6 switch(light){ 7 case'g':printf("go go go\n");break; 8 case'y':printf("wait a minute\n");break; 9 case'r':printf("stop!\n");break; 10 default:printf("something must be wrong\n");break; 11 } 12 } 13 return 0; 14 }
运行结果截图

task4
源代码
1 #include <stdio.h> 2 3 int main() { 4 double expense; 5 double total = 0.0; 6 double max = 0.0; 7 double min = 20000.0; 8 int count = 0; 9 printf("输入今日开销,直到输入 -1终止\n"); 10 while (1) { 11 scanf("%lf", &expense); 12 if (expense == -1) { 13 break; 14 } 15 if (expense <= 0 || expense > 20000) { 16 continue; 17 } 18 if (expense > max) max = expense; 19 if (expense < min) min = expense; 20 total += expense; 21 count++; 22 } 23 printf("今日累计消费总额:%.1f\n", total); 24 printf("今日最高一笔开销:%.1f\n", max); 25 printf("今日最低一笔开销:%.1f\n", min); 26 27 return 0; 28 }
运行结果截图

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

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


浙公网安备 33010602011771号