实验2

实验2-1

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<math.h>
 5 
 6 #define N 5
 7 #define N1 374
 8 #define N2 465
 9 
10 int main()
11 {
12     int number;
13     int i;
14     
15     srand(time(0) ); //以当前系统时间作为随机种子
16     
17     for(i = 0; i < N; ++i){
18         number = rand()%(N2 - N1 + 1) + N1;
19         printf("202383290386%04d\n",number);
20     } 
21     
22     return 0;
23 }

 

 

实验2-2

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<math.h>
 5 
 6 int main()
 7 {
 8     int light;
 9     while ((light = getchar())!=EOF){
10     
11     switch(light){
12         case 'r':printf("stop!\n");break;
13         case 'g':printf("go go go\n");break;
14         case 'y':printf("wait a minute\n");break;
15         default:printf("something must be wrong\n");break;  
16     }
17 }
18 return 0;
19 }

 

 

实验2-3

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<math.h>
 5 
 6 int main()
 7 {
 8     int min = 1;
 9     int max = 30;
10     int i,x;
11     printf("猜猜2023年11月哪一天会是你的lucky day\n");
12     printf("开始喽,你有三次机会,猜吧(1~30):"); 
13     
14     srand((unsigned)time(NULL));
15         int randomNum = min + (rand()% (max-min + 1));
16     for(i = 0;i<3;++i){
17     
18         scanf("%d",&x);
19         
20             if( x==randomNum )
21                   printf("哇 猜中了\n");
22             if( x<randomNum)
23             printf("你猜的日期早了,你的lucky day还没到呢\n");
24             if( x>randomNum )
25             printf("你猜的日期晚了,你的lucky day已经过啦\n");
26              
27     }
28     if(x!=randomNum)
29         printf("次数用完啦,偷偷告诉你:11月,你的lucky day是%d",randomNum);
30     
31     return 0;
32 }

 

posted @ 2023-10-16 17:28  Gustave-Ehsy  阅读(25)  评论(0)    收藏  举报