实验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 }

 

(1):生成一个在N1-N2的数值

(2):生成一个在1-N3的数值

(3):生成学号的随机数

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 a;
 5     while(scanf("%c",&a)!=EOF)
 6     {
 7         if(a=='g')
 8         {
 9             printf("gogogo\n"); 
10         }
11         else if(a=='y'){
12         printf("wait a minute\n");
13         }
14         else if(a=='r'){
15             printf("stop!\n");
16         }
17         else{
18             printf("something must went wrong\n");
19         }
20         getchar();
21     }
22     return 0

 

23  } 

 

Task4

 1 #include<stdio.h>
 2 int main()
 3 {
 4     double a,total,max,min;
 5     max=0;
 6     min=20000;
 7     total=0;
 8     while(scanf("%lf",&a)!=EOF&&a!=-1)
 9     {
10         if(a>max)
11         {
12             max=a;
13         }
14         if(a<min)
15         {
16             min=a;
17         }
18         total=total+a;
19     }
20     printf("最大开销=%lf\n",max);
21     printf("最低开销=%lf\n",min);
22     printf("总开销=%lf\n",total);
23     return 0;
24 }

 Task5

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

 Task6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include <time.h>
 4 #define N 3
 5 int main()
 6 {
 7     srand(time(NULL));
 8     int x,a,cnt;
 9     a=rand()%30+1;
10     cnt=0;
11     while(cnt<N){
12         scanf("%d",&x);
13         if(x<a){
14             printf("早了\n");
15         }
16         else if(x>a){
17             printf("晚了\n");
18         }
19         else if(x==a)
20         {
21             printf("猜中了!\n");
22             return 0; 
23         }
24         cnt+=1;
25     }
26         printf("下次加油,lucky day 是%d\n",a);
27     
28     return 0;
29 }

 

posted @ 2024-10-09 20:48  JackSusan'  阅读(17)  评论(0编辑  收藏  举报