实验2

task1

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 #define N1 397
 7 #define N2 476
 8 #define N3 21
 9 
10 int main() {
11     int cnt;
12     int random_major, random_no;
13 
14     srand(time(NULL));      
15 
16     cnt = 0;
17     while(cnt < N) {
18         random_major = rand() % 2;
19 
20         if(random_major) {
21             random_no = rand() % (N2 - N1 + 1) + N1;
22             printf("20248329%04d\n", random_no);
23         }
24         else {
25             random_no = rand() % N3 + 1;
26             printf("20248395%04d\n", random_no);
27         }
28 
29         cnt++;
30     }
31 
32     return 0;
33 }

问题回答

Q1:line21为了随机生成N1-N2的整数

Q2:line25为了随机生成1-N3的整数

Q3:该程序是为了随机生成该专业的五个学号。

task2

源代码

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main() {
 5     double a, b, c;
 6     double delta, p1, p2; 
 7 
 8     while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
 9         if(a == 0) {
10             printf("a = 0, invalid input\n");
11             continue;
12         }
13 
14         delta = b*b - 4*a*c;
15         p1 = -b/2/a;
16         p2 = sqrt(fabs(delta))/2/a;
17 
18         if(delta == 0)
19             printf("x1 = x2 = %.2g\n", p1);
20         else if(delta > 0)
21             printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2);
22         else {
23             printf("x1 = %.2g + %.2gi, ", p1, p2);
24             printf("x2 = %.2g - %.2gi\n", p1, p2);
25         }
26     }
27 
28     return 0;
29 }

task3

 

源代码

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

task4

源代码

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

task5

源代码

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

task6

源代码

 1 #include<stdio.h>
 2 #include<time.h>
 3 int main()
 4 {
 5     printf("猜猜2024年11月中哪一天会是你的lucky day吧\n");
 6     printf("开始猜咯,你有三次机会,猜吧(1~30):\n");
 7     srand(time(NULL));
 8     int luckyday = rand() % 30 + 1;
 9     int date;
10     int chance = 1;
11     
12     while(chance<=3){
13         scanf("%d",&date);
14         if(date<luckyday){
15         
16         printf("你猜的日期早了,你的lucky day还没到呢\n");
17         if (chance<=3) 
18         printf("再猜(1~30):\n");
19     }
20         else if(date>luckyday){
21         
22         printf("你猜的日期晚了,你的lucky day还在前面哦\n");
23         if(chance<=3)
24         printf("再猜(1~30):\n");    
25     }
26         else if(date==luckyday){
27             printf("哇,猜中了!");
28             break;
29         } 
30         chance++;
31     }
32     if ( chance== 4)
33         printf("次数用光啦。偷偷告诉你,11 月你的lucky day是%d号", luckyday);
34 
35     
36     return 0;
37 }
posted @ 2024-10-15 15:30  易香兑  阅读(5)  评论(0编辑  收藏  举报