作业二

task1

 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 
 9 int main(){
10     int cnt;
11     int random_major,random_no;
12 
13     srand(time(NULL));
14 
15     cnt=0;
16     while(cnt<N){
17         random_major=rand()%2;
18         if(random_major){
19         random_major=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     system("pause");
28 return 0;
29 }
View Code

1

Q1 生成一个随时间变化的随机数

Q2 从专业一的35人和专业二的80人中随机生成五个人的学号 其中不同专业的学号前缀不同

 

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("请输入饮料编号:\n");
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 
23         if(quantity<0){
24             printf("购买的数量不能为负数,请重新输入。\n");
25             continue;}
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 
33         printf("请投入金额:");
34         scanf("%f",&amount_paid);
35 
36         change=amount_paid-total_price;
37         printf("本次购买总价:%.2f元\n",total_price);
38         printf("找零:%.2f元\n",change);
39         total_price=0;
40     }
41     printf("感谢您的购买,欢迎下次光临!\n");
42     return 0;
43 }
View Code

2

  Q1 去掉会让总价保留这一次的数据 导致下一次循环无法重置 影响运行结果

  Q2 使循环重新开始 

 

task3

 1 #include<stdio.h>
 2 int main(){
 3     char ans,r,g,y;
 4 
 5     while(1){
 6     scanf("%c",&ans);
 7     getchar();
 8 
 9     if(ans=='r')
10         printf("stop!\n");
11     else if(ans=='g')
12         printf("go go go\n");
13     else if(ans=='y')
14         printf("wait a minute\n");
15     else
16         printf("something muast be wrong...\n");}
17     return 0;
18 }
View Code

3

 

task4

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

4

 

task5

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

5

 

task6

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

66

 

posted @ 2025-10-15 23:17  Novven  阅读(4)  评论(0)    收藏  举报