实验2
实验任务1

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 5 5 #define N1 80 6 #define N2 35 7 int main() { 8 int cnt; 9 int random_major, random_no; 10 11 srand(time(NULL)); // 以当前系统时间作为随机种子 12 13 cnt = 0; 14 while(cnt < N) { 15 random_major = rand() % 2; 16 17 if(random_major) { 18 random_no = rand() % N1 + 1; 19 printf("20256343%04d\n", random_no); 20 } 21 else { 22 random_no = rand() % N2 + 1; 23 printf("20256136%04d\n", random_no); 24 } 25 26 cnt++; 27 } 28 return 0; 29 }
问题1:根据时间随机生成一个数
问题2:随机抽取4个学号
实验任务2

1 #include <stdio.h> 2 3 int main() { 4 int choice, quantity; 5 float total_price = 0, amount_paid, change; 6 7 while (1) { 8 printf("\n自动饮料售卖机菜单:\n"); 9 printf("1. 可乐 - 3 元/瓶\n"); 10 printf("2. 雪碧 - 3 元/瓶\n"); 11 printf("3. 橙汁 - 5 元/瓶\n"); 12 printf("4. 矿泉水 - 2 元/瓶\n"); 13 printf("0. 退出购买流程\n"); 14 printf("请输入饮料编号: "); 15 scanf("%d", &choice); 16 17 if (choice == 0) 18 break; 19 20 if (choice < 1 || choice > 4) { 21 printf("无效的饮料编号,请重新输入。\n"); 22 continue; 23 } 24 25 printf("请输入购买的数量: "); 26 scanf("%d", &quantity); 27 28 if (quantity < 0) { 29 printf("购买数量不能为负数,请重新输入。\n"); 30 continue; 31 } 32 33 if(choice == 1 || choice == 2) 34 total_price += 3 * quantity; 35 else if(choice == 3) 36 total_price += 5 * quantity; 37 else 38 total_price += 2 * quantity; 39 40 printf("请投入金额: "); 41 scanf("%f", &amount_paid); 42 43 change = amount_paid - total_price; 44 printf("本次购买总价: %.2f 元\n", total_price); 45 printf("找零: %.2f 元\n", change); 46 47 total_price = 0; 48 } 49 50 printf("感谢您的购买,欢迎下次光临!\n"); 51 return 0; 52 }
问题1:重置前一次买东西的价钱
问题2:执行完前段语句后重复当前循环
实验任务3

1 #include <stdio.h> 2 3 int main() { 4 char x; 5 while(scanf("%c",&x)!=EOF) 6 { 7 8 if(x=='r') 9 { 10 printf("stop!\n"); 11 } 12 13 else if(x=='g') 14 { 15 printf("go go go\n"); 16 } 17 18 else if(x=='y') 19 { 20 printf("wait a minute\n"); 21 } 22 23 else 24 { 25 printf("something must be wrong\n"); 26 } 27 while(getchar()!='\n'); 28 29 } 30 return 0; 31 }
实验任务4

1 #include <stdio.h> 2 3 int main() { 4 double x,max=0,min=20000,t=0; 5 while(1) 6 { 7 scanf("%lf",&x); 8 if(x==-1) 9 {break;} 10 11 t=t+x; 12 if(max<x) 13 { 14 max=x; 15 } 16 if(min>x) 17 { 18 min=x; 19 } 20 21 } 22 23 printf("今日总消费:%.1f\n",t); 24 printf("今日最高消费:%.1f\n",max); 25 printf("今日最低消费:%.1f\n",min); 26 return 0; 27 }
实验任务5

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

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 srand(time(NULL)); 8 int i = rand() % 30 + 1; 9 int day, count = 0; 10 11 printf("猜猜2025年11月哪一天是你的lucky day\n"); 12 printf("开始搂,你有三次机会,猜吧!(1~30):"); 13 14 for(count = 0; count < 3; count++) 15 { 16 scanf("%d", &day); 17 18 if(day == i) 19 { 20 printf("wa,猜中了!\n"); 21 break; 22 } else if(day < i) 23 { 24 printf("你猜的日期早了,你的lucky day还没到呢\n"); 25 } else { 26 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 27 } 28 29 if(count < 2) 30 { 31 printf("再猜(1~30):"); 32 } 33 } 34 35 if(count == 3 && day != i) 36 { 37 printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", i); 38 } 39 40 return 0; 41 }