第二次实验作业

 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         
19         if(random_major){
20             random_no = rand() % N1 + 1;
21             printf("20256343%04d\n", random_no);
22         }
23         else{
24             random_no = rand() % N2 +1;
25             printf("20256136%04d\n",random_no);
26         }
27         
28         cnt++;
29     }
30     return 0;
31 } 

实验一代码及运行结果截图

问题一:因为代码srand(time(NULL))以系统时间作为随机种子,所以当短时间内重复运行程序的时候,若无此代码则生成的数字都一样。所以此代码的作用是无视时间。
问题二:这个程序的功能是随机生成五个不同的学号且重复生成的时候学号不一样

e8505a6c8ace996f9ce9b2a15e367233

实验二

问题一:total_price = 0的作用是刷新每次的购买总价,如果去掉则第一次购买物品的价格会加到第二次上面。
问题二:continue 语句的语义是提前结束本次循环进入新的循环
 
#include<stdio.h>

int main(){
    int choice, quantity;
    float total_price=0, amount_paid, change;
    
    while (1){
        printf("\n自动饮料售卖机菜单:\n");
        printf("1.可乐- 3 元/瓶\n");
        printf("2.雪碧- 3 元/瓶\n");
        printf("3.橙汁- 5 元/瓶\n");
        printf("4.矿泉水- 2 元/瓶\n");
        printf("0. 退出购买流程\n");
        printf("请输入饮料编号:");
        scanf("%d",&choice);
        
        if(choice == 0)
        break;
        
        if(choice<1||choice>4){
            printf("无效的饮料编号,请重新输入。\n");
            continue;
        }
        
        printf("请输入购买数量:");
        scanf("%d",&quantity);
        
        if(quantity<0){
            printf("购买数量不能为负数,请重新输入。\n");
            continue;
        }
        
        if(choice == 1 ||choice == 2)
        total_price += 3*quantity;
        else if(choice == 3)
        total_price += 5*quantity;
        else
        total_price+=2*quantity;
        
        printf("请输入金额:");
        scanf("%f",&amount_paid);
        
        change = amount_paid - total_price;
        printf("本次购买总价: %.2f 元\n",total_price);
        printf("找零:%.2f 元\n",change);
        
        total_price = 0; 
    }
    
    printf("感谢你的购买,欢迎下次光临!\n");
    return 0;
} 

65d1a175de0b1f67dd351d83bceb4f9b

 

#include<stdio.h>
   int main(){
       char n;
    while (1){
    scanf(" %c",&n);
    if(n=='y')
       printf("wait a minute\n");      
       else if(n=='r')
       printf("stop!\n");
       else if(n=='g')
       printf("go go go\n");
       else
       printf("something must be wrong....\n");
   }
   return 0;
   }

76a38e577f8f25e7a5693e145a4ca8d5

 

#include<stdio.h>
int main(){
   double n,max=0,min=20000,sum=0;
   printf("输入今日开销,直到输入-1终止:\n");
   while(1){
       scanf("%lf",&n);
       if(n==-1)
       break;
       sum+=n;
       if(n>max)
       max=n;
       if(n<min)
       min=n;}
    printf("今日累计消费总额:%.1f\n",sum);    
    printf("今日最高的一笔开销:%.1f\n",max);
    printf("今日最低的一笔开销:%.1f\n",min);

   return 0;
}

853d4a73abdaf8ea46004fcbe7d0b807

 

#include<stdio.h>
  int main(){
      int a,b,c;
      while (1){
          scanf("%d%d%d",&a,&b,&c);
          if(a+b<=c||b+c<=a||a+c<=b)
          printf("不能构成三角形\n");
          else if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b)
          printf("直角三角形\n");
          else if(a==b&&b==c)
          printf("等边三角形\n");
          else if(a==b&&b!=c||b==c&&c!=a||a==c&&c!=b)
          printf("等腰三角形\n");
          else
          printf("普通三角形\n");
      }
      return 0;
  }

3588ab54d42c00c5333fc547d0d9892d

 

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
  int main(){
  int luckyday,guessnum,cnt;
  cnt=0;
  srand(time(NULL));
  luckyday = rand()%30 + 1;
  printf("猜猜2025年11月哪一天是你的lucky day\n");
  printf("开始喽,你有三次机会,猜吧(1~30):");
  while(cnt<3){
  scanf("%d",&guessnum);
  if(guessnum==luckyday)
  printf("哇,猜中了:)\n");
  else if(guessnum>luckyday){
  printf("你猜的日期晚了,你的lucky day在前面哦\n");
  printf("再猜(1~30):");
  cnt++;}
  else{
      printf("你猜的日期早了,你的lucky day还没到呢\n");
      printf("再猜(1~30):");
      cnt++;
  }
  if(cnt>=3)
  printf("次数用光啦。偷偷告诉你,11月你的luckyday是%d号\n",luckyday);
}
return 0;
  }

de07eafa1d9f8e8d8d374d9f097a95e2

 


 


 

posted @ 2025-10-14 11:43  陆绎  阅读(9)  评论(0)    收藏  举报