实验二
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); } system("pause"); return 0; }

1 生成1到100的随机数
2把number固定为至少四位,不够四位的前面用0补齐
3生成并打印五个学员编号
实验2
#include <stdio.h> #include <stdlib.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; } switch (choice) { case 1: case 2: total_price += 3 * quantity; break; case 3: total_price += 5 * quantity; break; case 4: total_price += 2 * quantity; break; } printf("请投入金额: "); scanf("%f", &amount_paid); change = amount_paid - total_price; printf("本次购买总价: %.2f 元\n", total_price); printf("找零: %.2f 元\n", change); total_price = 0; } printf("感谢您的购买,欢迎下次光临!\n"); system("pause"); return 0; }

1每次购买后重置数据,去掉的话会每次使用累加,导致结果出错
2break为终止,Continue为跳过
3没有必要,line17到18已经正确输入并做了检查
实验三
#include <stdio.h> int main () { char symbol; while (scanf("%c",&symbol)!=EOF) {getchar(); switch(symbol) {case 'r': printf("stop!\n");break; case 'g': printf("go go go\n");break; case 'y': printf("wait a minute\n");break; default : printf("something must be wrong...\n"); } } return 0; }

实验4
#include <stdio.h> #include <stdlib.h> int main() {double max = 0, min = 20000, exp, all_exp = 0; printf("输入今日开销,直到输入-1终止:\n"); while (scanf("%lf",&exp), exp != -1) { if (exp<=0 ||exp >20000) continue; if (exp >= max) max = exp; if (exp <= min) min = exp; all_exp += exp; } printf("今日累计消费总额:%.1f\n", all_exp); printf("今日最高一笔开销:%.1f\n", max ); printf("今日最低一笔开销:%.1f\n", min ); system("pause"); return 0;

实验5
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { // 使用当前时间作为随机数种子 srand((unsigned int)time(NULL)); // 生成[1, 30]之间的随机整数作为lucky day int luckyDay = rand() % 30 + 1; int guess; int attempts = 3; printf("猜猜2025年4月哪一天是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30): "); while (attempts > 0) { scanf("%d", &guess); if (guess == luckyDay) { printf("哇,猜中了:-)\n"); break; } else if (guess < luckyDay) { printf("你猜的日期早了,你的lucky day还没到呢\n"); } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); } attempts--; if (attempts > 0) { printf("再猜(1~30): "); } } if (attempts == 0) { printf("次数用完啦。偷偷告诉你,4月你的lucky day是%d号\n", luckyDay); } return 0; }


实验6
#include <stdio.h> void printRow(int n, int spaces) { for (int i = 0; i < spaces; i++) { printf(" "); } for (int i = 0; i < n; i++) { printf("0 "); } printf("\n"); for (int i = 0; i < spaces; i++) { printf(" "); } for (int i = 0; i < n; i++) { printf("<H> "); } printf("\n"); for (int i = 0; i < spaces; i++) { printf(" "); } for (int i = 0; i < n; i++) { printf("I I "); } printf("\n"); } int main() { int n; printf("请输入 n: "); scanf("%d", &n); printf("input n: %d\n", n); for (int i = n; i > 0; i--) { printRow(i, n - i); } return 0; }


浙公网安备 33010602011771号