实验 2

task 1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 #define N1 397
 6 #define N2 476
 7 #define N3 21
 8 int main() {
 9 int cnt;
10 int random_major, random_no;
11 srand(time(NULL)); // 以当前系统时间作为随机种子
12 cnt = 0;
13 while(cnt < N) {
14 random_major = rand() % 2;
15 if(random_major) {
16 random_no = rand() % (N2 - N1 + 1) + N1;
17 printf("20248329%04d\n", random_no);
18 }
19 else {
20 random_no = rand() % N3 + 1;
21 printf("20248395%04d\n", random_no);
22 }
23 cnt++;
24 }
25 return 0;
26 }

问题1:使得生成的20248329(xxxx)范围在11 12班的学号内

问题2:使得生成的202483295(xxxx)范围在奇安信班的学号内

问题3:该程序实现了随机抽取11.12班.奇安信班的同学回答问题

 

task 2

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

 

 

task 3

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

 

 

task 4

 1 #include<stdio.h>
 2 int main(){
 3     printf("输入今日开销,直到输入-1终止:"); 
 4     double a[100];
 5     int i;
 6     double sum=0;
 7     int count=0;
 8     for(i=0;i<100;i++){
 9         scanf("%lf",&a[i]);
10         sum+=a[i];
11         if(a[i]==-1){
12             break;
13         }
14         count++;
15     }
16         double temp1=0;
17         double temp2=a[0];
18     for(i=0;i<count;i++){
19         
20         if(a[i]>temp1){
21             temp1=a[i];
22             
23         }
24     }
25     
26     for(i=1;i<count;i++){
27         if(a[i]<temp2&&a[i]!=-1){
28             temp2=a[i];
29         }
30     }
31     printf("今日累计消费总额%.1f\n",sum); 
32     printf("今日最高一笔开销%.1f\n",temp1);
33     printf("今日最低一笔开销%.1f\n",temp2);
34     
35     return 0;
36 }

 

 

task 5

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

 

 

task 6

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

 

 

posted @ 2024-10-11 01:05  202483290420王璐  阅读(5)  评论(0编辑  收藏  举报