程序1
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #define N 5 6 #define N1 80 7 #define N2 35 8 int main() { 9 int cnt; 10 int random_major, random_no; 11 srand(time(NULL)); // 以当前系统时间作为随机种子 12 cnt = 0; 13 while (cnt < N) { 14 random_major = rand() % 2; 15 if (random_major) { 16 random_no = rand() % N1 + 1; 17 printf("20256343%04d\n", random_no); 18 } 19 else { 20 random_no = rand() % N2 + 1; 21 printf("20256136%04d\n", random_no); 22 } 23 cnt++; 24 } 25 return 0; 26 }
问题1:srand(time(NULL))是在时间范围内随机取数,以生成随机数字。
问题2:这个程序是用来随机生成学号的。
程序2:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int main() { 4 int choice, quantity; 5 float total_price = 0, amount_paid, change; 6 while (1) { 7 printf("\n自动饮料售卖机菜单:\n"); 8 printf("1. 可乐 - 3 元/瓶\n"); 9 printf("2. 雪碧 - 3 元/瓶\n"); 10 printf("3. 橙汁 - 5 元/瓶\n"); 11 printf("4. 矿泉水 - 2 元/瓶\n"); 12 printf("0. 退出购买流程\n"); 13 printf("请输入饮料编号: "); 14 scanf("%d", &choice); 15 if (choice == 0) 16 break; 17 if (choice < 1 || choice > 4) { 18 printf("无效的饮料编号,请重新输入。\n"); 19 continue; 20 } 21 printf("请输入购买的数量: "); 22 scanf("%d", &quantity); 23 if (quantity < 0) { 24 printf("购买数量不能为负数,请重新输入。\n"); 25 continue; 26 } 27 if (choice == 1 || choice == 2) 28 total_price += 3 * quantity; 29 else if (choice == 3) 30 total_price += 5 * quantity; 31 else 32 total_price += 2 * quantity; 33 printf("请投入金额: "); 34 scanf("%f", &amount_paid); 35 change = amount_paid - total_price; 36 printf("本次购买总价: %.2f 元\n", total_price); 37 printf("找零: %.2f 元\n", change); 38 total_price = 0; 39 } 40 printf("感谢您的购买,欢迎下次光临!\n"); 41 return 0; 42 }
![2]()
问题1:去掉47行后每次循环total_price并不会被重置,导致找零出错。
问题2:第一处continue表示在判断出choice不在1到4之间时结束此次循环并开始下一次循环,而第二处continue是在判断出quantity<0时结束循环并从头开始循环。
程序3:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int main() { 4 char ch; 5 while (scanf("\n%c", &ch) != EOF) { 6 if (ch == 'r') { 7 printf("stop!\n"); 8 } 9 if (ch == 'g') { 10 printf("go go go\n"); 11 } 12 if (ch == 'y') { 13 printf("wait a minute\n"); 14 } 15 if (ch != 'r' && ch != 'g' && ch != 'y') { 16 printf("something must be wrong...\n"); 17 } 18 } 19 return 0; 20 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int main() { 4 double a, sum, max, min; 5 sum = 0; 6 max = 0; 7 min = 20000; 8 printf("请输入今日开销,直到输入-1终止:\n"); 9 while (scanf("%lf", &a) != EOF) { 10 if (a == -1) { 11 break; 12 } 13 if (a > 0 && a <= 20000) { 14 sum += a; 15 if (a >max) { 16 max = a; 17 } 18 if (a <min) { 19 min = a; 20 } 21 } 22 } 23 printf("今日累计消费总额:%.1lf\n", sum); 24 printf("今日最高一笔开销:%.1lf\n", max); 25 printf("今日最低一笔开销:%.1lf\n", min); 26 return 0; 27 }
程序5:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int main() { 4 int a, b, c; 5 while (scanf("%d%d%d", &a, &b, &c) != EOF) { 6 if (a + b > c && a + c > b && b + c > a) { 7 if (a == b &&b== c) 8 printf("等边三角形"); 9 10 else if (a == b && b != c || a == c && c != b || b == c && c != a) 11 printf("等腰三角形"); 12 13 else if (a * a == b * b + c * c || b * b == c * c + a * a || c * c == a * a + b * b) 14 printf("直角三角形"); 15 else 16 printf("普通三角形"); 17 } 18 19 if(a+b<=c||a+c<=b||b+c<=a) 20 printf("不能构成三角形"); 21 22 23 24 } 25 return 0; 26 }
程序6:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include<stdlib.h> 4 #include<time.h> 5 #define N 3 6 int main() { 7 int cnt=0; 8 int a, random; 9 srand(time(NULL)); 10 random = rand() % 30 + 1; 11 12 printf("猜猜2025年11月哪一天是你的lucky day\n"); 13 printf("开始喽,你有三次机会,猜吧(1-30):"); 14 15 16 do { 17 scanf("%d", &a); 18 19 20 if (a == random) { 21 printf("哇,猜中了"); 22 break; 23 } 24 if (a != random) { 25 if (a < random) { 26 printf("你猜的日期早了,你的lucky day还没到呢\n"); 27 printf("再猜(1-30):"); 28 29 } 30 if (a > random) { 31 printf("你猜的日期晚了,你的lucky day在前面呢\n"); 32 printf("再猜(1-30):"); 33 34 } 35 36 cnt++; 37 } 38 39 } while (cnt < 3); 40 if (cnt == 3) { 41 printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号", random); 42 43 } 44 return 0; 45 }