实验2作业

实验一:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 #define N1 397
 7 #define N2 376
 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 
25         else{
26             random_no = rand() % N3 + 1;
27         }
28         
29         cnt++;
30     }
31     
32     return 0;
33 }

结果:

实验二:

 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("%1f%1f%1f", &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 }

结果:

实验三:

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

结果:

实验四:

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

结果:

实验五:

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

结果:

实验六:

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

结果:

 

posted @ 2024-10-15 17:12  九泪纵横  阅读(2)  评论(0编辑  收藏  举报