任务一:
#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++; } return 0; }
结果:
问题一:
srand(time(NULL))使得每次生成随机数时的种子不同,从而生成不同的随机数,如果去掉这一设置,伪随机数生成器只会播种一次,使得每次生成的随机数都与上一次结果相同。
问题二:
该程序通过伪随机数生成器生成一个数,通过判断奇偶对random_no进行不同的操作,输出不同的结果,此过程通过while循环5次。
任务二:
#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; }
结果:
问题一:
去掉total_price=0,不重置total_price的值,程序会保存此次total_price的值,从而影响之后的结果
问题二:
continue:跳出当此循环,继续进行下一次循环
任务三:
#include<stdio.h> int main(){ char s; while(scanf("%c",&s)!=EOF){ if(s=='r'){ printf("stop!\n"); getchar(); } else if(s=='g'){ printf("go go go\n"); getchar(); } else if(s=='y'){ printf("wait a minute\n"); getchar(); } else{ printf("something must be wrong...\n"); getchar(); } } return 0; }
实验结果:
任务四:
#include<stdio.h> int main(){ printf("输入今日开销,直到输入-1为止:\n"); float cost=0.0,max_cost=0.0,min_cost=20000.0,total_cost=0.0; while(scanf("%f",&cost)!=EOF && cost!=-1){ if(cost>max_cost){ max_cost = cost; } if(cost<min_cost){ min_cost = cost; } total_cost += cost; } printf("今日消费总额:%.1f\n", total_cost); printf("今日最高一笔开销:%.1f\n", max_cost); printf("今日最低一笔开销:%.1f\n", min_cost); return 0; }
实验结果:
任务五:
#include<stdio.h> int main(){ int a, b, c; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ if(a>0&&b>0&&c>0&&a+b>c&&a+c>b&&b+c>a){ if(a==b&&b==c){ printf("等边三角形\n"); } else if(a==b||a==c||b==c){ printf("等腰三角形\n"); } else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ printf("直角三角形\n"); } else{ printf("普通三角形\n"); } } else{ printf("不能构成三角形\n"); } } return 0; }
结果:
任务六:
#include <stdio.h> #include<stdlib.h> #include<time.h> int main() { int rand_num; srand(time(NULL)); rand_num = rand() % 30 + 1; int tried_count = 0, day; printf("猜猜2025年11月哪一天是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); while (tried_count < 3) { scanf("%d", &day); if (day < rand_num) { printf("你猜的日期早了,你的lucky day还没到呢\n"); printf("再猜(1~30):"); } else if (day > rand_num) { printf("你猜的日期晚了,你的lucky day在前面哦\n"); printf("再猜(1~30):"); } else { printf("哇,猜中了:)\n"); return 0; } tried_count++; } printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号\n", rand_num); return 0; }
结果: