task1.c源代码
点击查看代码- #include<stdlib.h>
- #include<time.h>
- #define N 5
- #define N1 80
- #define N2 35
- int main(){
- int cnt;
- int random_majior, random_no;
- srand(time(NULL));
-
- cnt = 0;
- while (cnt < N) {
- random_majior = rand() % 2;
- if (random_majior) {
- 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))的作用:设置随机数种子,使生成的数字完全随机
程序的作用:随机生成两个专业的学号中的5个学号
task2.c源代码
点击查看代码- #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;
- }
复制代码 运行结果截图
回答问题
若去掉line 47,会引发程序异常导致中止
continue的作用:继续循环中的下一步
task3.c源代码
点击查看代码- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- int main() {
- char color;
- while (1) {
- scanf("%c", &color);
- getchar();
- if (color == 'y')
- {
- printf("wait a minute\n");
- }
- else if (color == 'r')
- {
- printf("stop!\n");
- }
- else if (color == 'g')
- {
- printf("go go go\n");
- }
- else
- {
- printf("something must be wrong...\n");
- }
- }
- return 0;
- }
复制代码 运行结果截图
task4.c源代码
点击查看代码- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- int main() {
- double sum = 0, max_num = 0, min_num = 20000, num;
- printf("输入今日开销,直到输入-1时终止:\n");
- while (1) {
- scanf("%lf", &num);
- if (num > max_num) {
- max_num = num;
- }
- if (num > 0 && num < min_num) {
- min_num = num;
- }
- sum += num;
- if (num == -1)
- break;
- }
- printf("今日累计消费总额:%.1lf\n", sum);
- printf("今日最高一笔开销:%.1lf\n", max_num);
- printf("今日最低一笔开销:%.1lf\n", min_num);
- return 0;
- }
复制代码 运行结果截图
task5.c源代码
点击查看代码[code]#includeint main() { int a, b, c; while (1) { scanf("%d %d %d", &a, &b, &c); if(a + b |