实验2

1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 
 7 int main() {
 8     int number;
 9     int i;
10 
11     srand(time(0)); // 以当前系统时间作为随机种子
12     for (i = 0; i < N; ++i) {
13         number = rand() % 100 + 1;
14         printf("20490042%04d\n", number);
15     }
16 
17     return 0;
18 }

2

 

2

 1 #include <stdio.h>
 2 int main() {
 3     int choice, quantity;
 4     float total_price = 0, amount_paid, change;
 5     while (1) {
 6         printf("\n自动饮料售卖机菜单:\n");
 7         printf("1. 可乐 - 3 元/瓶\n");
 8         printf("2. 雪碧 - 3 元/瓶\n");
 9         printf("3. 橙汁 - 5 元/瓶\n");
10         printf("4. 矿泉水 - 2 元/瓶\n");
11         printf("0. 退出购买流程\n");
12         printf("请输入饮料编号: ");
13         scanf_s("%d", &choice);
14         if (choice == 0)
15             break;
16         if (choice < 1 || choice > 4) {
17             printf("无效的饮料编号,请重新输入。\n");
18             continue;
19         }
20         printf("请输入购买的数量: ");
21         scanf_s("%d", &quantity);
22         if (quantity < 0) {
23             printf("购买数量不能为负数,请重新输入。\n");
24             continue;
25         }
26         if (choice == 1 || choice == 2)
27             total_price += 3 * quantity;
28         else if (choice == 3)
29             total_price += 5 * quantity;
30         else
31             total_price += 2 * quantity;
32         printf("请投入金额: ");
33         scanf_s("%f", &amount_paid);
34         change = amount_paid - total_price;
35         printf("本次购买总价: %.2f 元\n", total_price);
36         printf("找零: %.2f 元\n", change);
37         total_price = 0;
38     }
39     printf("感谢您的购买,欢迎下次光临!\n");
40     return 0;
41 }

3

 

3

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char symble;
 5     while (scanf_s("%c", &symble) != EOF) {
 6         if (symble == '\n') {
 7             continue;
 8         }
 9         switch (symble)
10         {
11         case'r':
12             printf("stop\n");
13             break;
14         case'g':
15             printf("go go go\n");
16             break;
17         case'y':
18             printf("wait a minute\n");
19             break;
20         default:
21             printf("something must be wrong\n");
22             break;
23         }
24     }
25     return 0;
26 }

4

 

4

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main()
 4 {
 5     double sum = 0.0, max = 0.0, min = 20000.0, n;
 6     printf("一天内的若干笔开销:\n");
 7 
 8     while (1) {
 9         scanf_s("%lf", &n);
10         if (n == -1) {
11             break;
12         }
13         if (n <= 0 || n >= 20000) {
14             printf("输入无效,输入0<n<20000的数值\n");
15             continue;
16         }
17         sum += n;
18         if (n > max) {
19             max = n;
20         }
21         if (n < min) {
22             min = n;
23         }
24     }
25     printf("最高一笔开销:%.1f\n",max);
26     printf("最低一笔开销:%.1f\n",min);
27     printf("一天总开销:%.1f\n",sum);
28     return 0;
29 }

5

 

6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int main() {
 5     int a, b, c;
 6     while (scanf_s("%d %d %d", &a, &b, &c) != EOF) {
 7         if (a + b <= c || a + c <= b || b + c <= a) {
 8             printf("不能构成三角形\n");
 9         }
10         else if (a == b && b == c) {
11             printf("等边三角形\n");
12         }
13         else if (a == b || a == c || b == c) {
14             printf("等腰三角形\n");
15         }
16         else if (a*a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) {
17             printf("直角三角形\n");
18         }
19         else {
20             printf("普通三角形\n");
21         }
22     }
23     return 0;
24 }

6

 

7

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main()
 6 {
 7     int lucky_day;
 8     int guess;
 9     int n;
10 
11     srand((unsigned)time(NULL));
12     lucky_day = rand() % 30 + 1;
13     n = 0;
14 
15     printf("猜猜2026年4月哪一天是你的lucky day\n");
16     printf("开始喽,你有3次机会,猜吧(1-30):");
17 
18     while(n < 3)
19     {
20         scanf("%d", &guess);
21         n++;
22 
23         if(guess == lucky_day)
24         {
25             printf("哇,猜中了:)\n");
26             system("pause");
27             return 0;
28         }
29         else if(guess > lucky_day)
30             printf("你猜的日期晚了,你的lucky day在前面喔\n");
31         else
32             printf("你猜的日期早了,你的lucky day还没到呢\n");
33 
34         if(n < 3)
35             printf("再猜(1-30):");
36     }
37 
38     printf("次数用光啦,4月你的lucky day是%d号\n", lucky_day);
39     system("pause");
40     return 0;
41 }

7

 

posted @ 2026-04-13 23:36  JRui-  阅读(6)  评论(0)    收藏  举报