实验2
1
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int number; int i; srand(time(0)); for(i = 0; i < N; ++i) { number = rand() % 100 + 1; printf("20490042%04d\n", number); } return 0; }

问题1.1 在202400420001到202400420100之间随机选择5个学号
1.2 生产1到100之间的随机整数
1.3让输出的随机数占据4个空格,不足4位左侧补0
1.4设置随机数种子为当前系统时间,是每次运行能生成不同的随机数序列,去掉这一行多次运行的效果相同
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; 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; }

问题2.1 去掉后总价会叠加
2.2表示跳过本次循环剩余代码,但是可以进入下一阶段
3
#include <stdio.h> int main() { char ch; while (scanf("%c", &ch) != EOF) { if (ch == 'r') { printf("stop!\n"); } else if (ch == 'g') { printf("go go go\n"); } else if (ch == 'y') { printf("wait a minute\n"); } else { if (ch != '\n') { printf("something must be wrong...\n"); } } } return 0; }

4
#include <stdio.h> int main() { double expense; double total = 0.0; double max_expense = 0.0; double min_expense = 20000.0; int count = 0; printf("输入今日开销,直到输入-1终止:\n"); while (scanf("%lf", &expense) == 1) { if (expense == -1) { break; } if (expense <= 0 || expense > 20000) { printf("输入无效!请输入0到20000之间的数值(输入-1终止)\n"); continue; } total += expense; count++; if (expense > max_expense) { max_expense = expense; } if (expense < min_expense) { min_expense = expense; } } if (count > 0) { printf("今日累计消费总额:%.1f\n", total); printf("今日最高一笔开销:%.1f\n", max_expense); printf("今日最低一笔开销:%.1f\n", min_expense); } else { printf("未输入有效开销!\n"); } return 0; }

5
#include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main() { int a, b, c; while (scanf("%d%d%d", &a, &b, &c) != EOF) { if (a > b) swap(&a, &b); if (b > c) swap(&b, &c); if (a > b) swap(&a, &b); if (a + b <= c) { printf("不能构成三角形\n"); continue; } if (a == b && b == c) { printf("等边三角形\n"); } else if (a == b || b == c || a == c) { printf("等腰三角形\n"); } else if (a * a + b * b == c * c) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } return 0; }

6
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int lucky_day, guess;
int chance = 3;
srand((unsigned int)time(NULL));
lucky_day = rand() % 30 + 1;
printf("猜猜2026年4月哪一天是你的lucky day\n");
while (chance > 0) {
printf("开始喽,你有%d次机会,猜吧(1~30): ", chance);
scanf("%d", &guess);
if (guess == lucky_day) {
printf("哇,猜中了:)\n");
return 0;
} else if (guess > lucky_day) {
printf("你猜的日期晚了,你的lucky day在前面哦\n");
} else {
printf("你猜的日期早了,你的lucky day还没到呢\n");
}
chance--;
if (chance > 0) {
printf("再猜(1~30): ");
scanf("%d", &guess);
if (guess == lucky_day) {
printf("哇,猜中了:)\n");
return 0;
} else if (guess > lucky_day) {
printf("你猜的日期晚了,你的lucky day在前面哦\n");
} else {
printf("你猜的日期早了,你的lucky day还没到呢\n");
}
chance--;
if (chance > 0) {
printf("再猜(1~30): ");
scanf("%d", &guess);
if (guess == lucky_day) {
printf("哇,猜中了:)\n");
return 0;
} else if (guess > lucky_day) {
printf("你猜的日期晚了,你的lucky day在前面哦\n");
} else {
printf("你猜的日期早了,你的lucky day还没到呢\n");
}
chance--;
}
}
}
printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day);
return 0;
}
浙公网安备 33010602011771号