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

C__Users_johnwzq_Documents_exp2.exe 2025_10_19 16_26_26

1.去掉会使输出结果不变

2.先等可能选取一个专业,再等可能从专业组选择人员,共选5人次。

这个代码缺陷非常明显。首先不保证不会重复选择,是放回式的抽样,其次分组抽样不按比例,导致人少的组任一组员更易被选中。我会跟进补充改进的代码。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 #define N1 80
 7 #define N2 35
 8 int main(){
 9     int cnt;
10     int r;//random_major,random_no;
11 
12     srand(time(NULL));
13 //new:未被选的人员数 
14     int u1=N1,u2=N2;
15     cnt=0;
16     while(cnt < N) {
17 //        random_major = rand()%2;
18 //        if(random_major){
19 //            random_no = rand()%N1+1;
20 //            printf("20256343%04d\n",random_no);
21 //        }else{
22 //            random_no=rand()%N2+1;
23 //            printf("20256136%04d\n",random_no);
24 //        }
25 //        cnt++;
26         //假如我想等比例分层抽样,同时防止重复,可以:
27         cnt++;
28         r=rand()%(u1+u2)+1;//[1,u1+u2]
29         if(r<=u1){//[1,u1]
30             printf("20256343%04d\n",r);
31             u1--;
32         }else{//[u1+1,u1+u2]
33             printf("20256136%04d\n",r-u1);
34             u2--;
35         } 
36     }
37     return 0;
38 }

 

实验任务二

 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  }

 

 

1.会导致上一人总价格累加到下一个人的总价里

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("%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         float change= -total_price;//change可能是差价(-)或找零(+)    
33         while(1){
34             printf("请投入金额: ");
35             scanf("%f", &amount_paid);
36             change += amount_paid;
37             if(change<0){//钱不够 
38                 printf("本次购买总价: %.2f 元\n", total_price);
39                 printf("您已付%.2f元,请继续付款%.2f元\n", total_price + change,-change);
40             }else{//钱有多余或恰好 
41                 printf("本次购买总价: %.2f 元\n", total_price);
42                 printf("找零: %.2f 元\n", change);
43                 break;
44             }
45         }    
46         
47         
48         total_price = 0;
49     }
50     printf("感谢您的购买,欢迎下次光临!\n");
51     return 0;
52  }

row 33-45

效果:

C__Users_johnwzq_Documents_yinliao improved.exe 2025_10_20 16_46_38

 

实验任务三

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     while(true){
 5         char signal=getchar();
 6         if(signal=='r')printf("Stop!\n");
 7         else if(signal=='y')printf("Wait a minute\n");
 8         else if(signal=='g')printf("Go go go!\n");
 9         else printf("Something must be wrong...\n");
10         getchar();
11     }
12     return 0;
13 }

C__Users_johnwzq_Documents_exp2.exe 2025_10_19 17_19_10

实验任务四

 1 #include <bits/stdc++.h>
 2 double ma=0,mi=30000,su=0,a;
 3 int main(){
 4     printf("输入今日开销,直到输入-1终止:\n");
 5     while(1){
 6         scanf("%lf",&a);
 7         if(a==-1){
 8             printf("今日累计消费总额:%.1lf\n",su);
 9             printf("今日最高一笔开销:%.1lf\n",ma);
10             printf("今日最低一笔开销:%.1lf\n",mi);
11             return 0;
12         }
13         su+=a;
14         ma=(ma>a?ma:a);
15         mi=(mi<a?mi:a);
16     } 
17     
18 }

C__Users_johnwzq_Documents_exp2.exe 2025_10_19 18_15_55

注意,初始值:sum=0,mul=1,min=很大的数,max=0或更小......

实验任务五

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

注意各个概念的内涵外延的逻辑包含关系

实验任务六

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

C__Users_johnwzq_Documents_exp2(2).exe 2025_10_20 15_51_51