202413350081刁嘉怡博客园2

TASK1


 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 }
task1

 

ANS1
随机生成1~100之间的整数
ANS2
指定4位宽度输出,不足用0填充
ANS3
随机抽取5个学号后3位在1~100的学生学号

 

TASK2

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

ANS1
将上一次的总价清零。去掉之后下一次购买时的总价会加上之前的总价
ANS2
使用break会直接跳转到该框内的程序末尾,在这里也就是直接结束程序;continue使用后会继续重新运行框内程序,直到手动结束
ANS3
没有必要。前面的case语句已经完全涵盖取值并被明确处理

 

TASK3

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

 

TASK4

 

 1 #include <stdio.h>
 2 
 3 int main() {
 4     double exp;
 5     double total=0;
 6     double max=0;
 7     double min =20000;
 8     
 9     printf("输入今日开销,直到输入-1终止:\n");
10     while(1){
11             scanf("%lf",&exp);
12             if(exp==-1){
13             break;
14             }
15             total+=exp;
16             if(exp>max){
17                 max=exp;
18             }
19             if(exp<min){
20                 min=exp;
21             }
22             
23         }
24         printf("今日累计消费总额:%.1f\n",total);
25     
26         printf("今日最高一笔开销:%.1f\n",max);
27         
28         printf("今日最小一笔开销:%.1f\n",min);
29     
30    
31     return 0;
32 }
t4

 

 

TASK5

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main() {
 6    srand((unsigned int)time(NULL));
 7    int Lucky=rand()%30+1;
 8    int yh , chance = 3;
 9    printf("猜猜25年4月哪一天是你的lucky day\n");
10    while(chance > 0){
11            printf("开始咯,你有3次机会,猜吧(1~30):\n");
12            scanf("%d",&yh);
13            if (yh < Lucky){
14                printf("你猜的日期早了,你的lucky day还没到呢\n,再猜");
15                } 
16         if (yh > Lucky){
17             printf("你猜的日期晚了,你的lucky day在前面呢\n,再猜");
18             } 
19         if (yh == Lucky){
20             printf("猜中了!\n");
21             return 0;
22             } 
23         
24         chance--; 
25    }
26    
27    printf("次数用完了,告诉你,幸运日是&yh 号");
28     return 0;
29 }
t5

 

 

TASK6

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int a,i,m,n;
 6     printf("input n: ");
 7     scanf("%d",&n);
 8 
 9     for(i=1;i<=n;++i){
10 
11         for(m = n - i + 1;m<n;++m)
12             printf("\t");
13 
14         for(a=0;a<2*(n-i)+1;++a)
15             printf(" O \t");
16         printf("\n");
17         
18         for(m=n-i+1;m<n;++m)
19             printf("\t");
20 
21         for(a=0;a<2*(n-i)+1;++a)
22             printf("<H>\t");
23         printf("\n");
24 
25         for(m=n-i+1;m<n;++m)
26             printf("\t");
27 
28         for(a=0;a<2*(n-i)+1;++a)
29             printf("I I\t");
30         printf("\n");
31     }
32     system("pause");
33     return 0;
34 }
t6

 

 

posted @ 2025-03-22 17:28  diaojiayi  阅读(21)  评论(0)    收藏  举报