任务1
#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++;}system("pause");return 0; }
多次运行代码,生成的数据不同
该代码的作用是获取随时在变的随机数
该程序的作用是随机获取几组学号数据
任务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;elsetotal_price += 2 * quantity;printf("请投入金额: ");scanf("%f", &amount_paid);change = amount_paid - total_price;printf("本次购买总价: %.2f 元\n", total_price);printf("找零: %.2f 元\n", change);}printf("感谢您的购买,欢迎下次光临!\n");total_price = 0;return 0; }
问题1:去掉后,下一次循环时总价格不为零,初始值会是上一次的价格
问题2:continue的语义是中止执行后面的代码并重新开始新循环
任务3
#include <stdio.h> int main() {char a;while (1) {scanf("%c", &a);if (a == 'r')printf("stop!\n");else if (a == 'y')printf("wait a minute\n");else if (a == 'g')printf("go go go\n");elseprintf("something must be wrong...\n");scanf("%c",&a);}return 0;}
任务4
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() {printf("输入今日开销,直到输入-1中止\n");double max, min,total, a;max = 0;min = 20000;total = 0;char b;while (1) {scanf("%lf", &a);if (a != -1){if (a > max)max = a;if (a < min)min = a;total += a;}elsebreak;}printf("今日累计消费总额:%.01lf\n", total);printf("今日最高一笔开销:%.01lf\n", max);printf("今日最低一笔开销:%.01lf\n", min);return 0;}
任务5
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() {int a, b, c;while (1) {scanf("%d%d%d", &a, &b, &c);if (a + b <= c || a + c <= b || b + c <= a)printf("不能构成三角形\n");else {if (a * a + b * b == c * c || c * c + b * b == a * a || a * a + b * b == c * c)printf("直角三角形\n");else if (a == b && b == c)printf("等边三角形\n");else if (a == b && b != c || b == c && a != b || a == c && c != b)printf("等腰三角形\n");elseprintf("普通三角形\n");}}return 0;}
任务6
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() {int a, b,c;srand(time(NULL));a = rand()%30+1;printf("猜猜2025年11月哪一天是你的lucky day\n");printf("你有3次机会,猜吧:");for (c = 1; c <= 3; c++){scanf("%d",&b);if (a == b) {printf("哇,猜中了:)");return 0;}if (a <= b)printf("你猜的时间晚了,你的lucky day在前面哦\n");elseprintf("你猜的时间早了,你的lucky day还没到呢\n");if (c<3)printf("再猜:");}printf("次数用光了,偷偷告诉你,11月你的lucky day是:%d", a);return 0; }