实验2

任务1:

 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:从11和12班中抽学号

Q2:从奇安信班中抽人

Q3:一共抽5人

任务2:

 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 }

任务3:

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

任务4:

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

任务5:

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

 任务6:

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

 

posted @ 2024-10-11 14:19  jjjrrhhhhh  阅读(6)  评论(0编辑  收藏  举报