实验2

task1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 #define N1 80
 6 #define N2 35
 7 int main() {
 8 int cnt;
 9 int random_major, random_no;
10 srand(time(NULL));
11 cnt = 0;
12 while(cnt < N) {
13       random_major = rand() % 2;
14     if(random_major) {
15       random_no = rand() % N1 + 1;
16       printf("20256343%04d\n", random_no);
17     }
18     else {
19       random_no = rand() % N2 + 1;
20       printf("20256136%04d\n", random_no);
21     }
22     cnt++;
23   }
24 return 0;
25 }  

image

srand(time(NULL))的作用是根据系统时间取伪随机数

程序功能为从两个班里随机抽取五个学号,随机数为奇数则取20256343,后两位在0-79取余+1,中间两位补00;随机数为偶数则取20256136,后两位在0-34取余+1,中间两位补00。


task2
 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("%d", &choice);
14     if (choice == 0)
15       break;
16     if (choice < 1 || choice > 4) {
17       printf("无效的饮料编号,请重新输入。\n");
18       continue;
19    }
20     printf("请输入购买的数量: ");
21     scanf("%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("%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 }

image

 删去total-price则下次运行会加上上次运行的total-price值

continue语义为跳出当次循环,继续下一次循环

 

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

image

 

task4
 1 #include <stdio.h>
 2 int main() {
 3     double a,max,min,total;
 4     int count;
 5     count = 0;
 6     a = 0;
 7     total = 0;
 8     printf("输入今日开销,直到输入-1终止:\n");
 9     
10     
11     while(1){
12         scanf("%lf",&a);
13         
14         if(a>0&&a<=20000){
15             if(count == 0){
16                 max = a;
17                 min = a;
18             }
19             else{
20                 if(max<=a){
21                     max = a;   
22                 }   
23                 else if(min>=a){
24                     min = a;
25                 }
26                 
27             }
28             total += a;
29             count++;
30         }
31         else if(a==-1){
32             printf("今日累计消费总额:%.1lf\n",total);
33             printf("今日最高一笔开销:%.1lf\n",max);
34             printf("今日最低一笔开销:%.1lf\n",min);
35             break;
36         }
37         else{
38             printf("输入数据有误,请重新输入:\n");
39             continue;
40         }
41     }
42     return 0;
43 }

image

 

task5

 

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

image

 

task6
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 int main() {
 5     srand(time(NULL));
 6     // 公式:[min, max] = rand() % (max - min + 1) + min
 7     int lucky_day =rand()%(30-1+1)+1;
 8     int gus,i;
 9     i = 0;
10     printf("猜猜2025年11月哪一天是你的lucky day\n");
11     printf("开始喽,你有三次机会,猜吧:");
12     while(i<3){
13         i++;
14         scanf("%d",&gus);
15         if(gus==lucky_day){
16             printf("哇,猜中了:)\n");
17             break;
18         }
19         else if(gus<lucky_day){
20             printf("你猜的日期早了,你的lucky day还没到呢\n");        
21         }
22         else if(gus>lucky_day){
23             printf("你猜的日期晚了,你的lucky day在前面哦\n");        
24         }
25         if(i==3){
26             printf("次数用光啦。偷偷告诉你,11月的lucky day是%d号",lucky_day);    
27         }
28         else{
29             printf("再猜(1~30):");    
30         }
31     }
32     return 0;
33 }

image

image

 

 

 
posted @ 2025-10-14 19:48  屑乃  阅读(10)  评论(1)    收藏  举报