作业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 }

问题一:生成N1到N2之间的随机整数

问题二:生成1到N3之间的随机整数

问题三:生成随机数

实验任务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 
 3 int main() {
 4     char c;
 5     int result;
 6 
 7     while (1) {
 8         result = scanf("%c", &c);
 9 
10         if (result == EOF) {
11             printf("程序结束\n");
12             break;
13         }
14 
15         getchar();
16 
17         if (c == 'r') {
18             printf("stop!\n");
19         } else if (c == 'g') {
20             printf("go go go\n");
21         } else if (c == 'y') {
22             printf("wait a minute\n");
23         } else {
24             printf("something must be wrong...\n");
25         }
26     }
27 
28     return 0;
29 }

运行结果

实验任务4

源代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     float a, b = 0.0, c = 20000.0, d = 0.0;
 5     int firstInput = 1;
 6     
 7     printf("请输入一天内的若干笔开销,输入-1时终止程序:\n");
 8 
 9     while (1) {
10         scanf("%f", &a);
11         
12         if (a == -1) {
13             break;
14         }
15 
16         d += a;
17 
18         if (firstInput || a > b) {
19             b = a;
20             firstInput = 0;
21         }
22 
23         if (a < c) {
24             c = a;
25         }
26     }
27 
28     printf("一天内最高一笔开销:%.1f\n", b);
29     printf("一天内最低一笔开销:%.1f\n", c);
30     printf("一天总开销:%.1f\n", d);
31 
32     return 0;
33 }

实验结果

实验任务5

源代码

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

实验结果

实验任务6

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main() {
 6     int luckyDay, guess;

7 int attempts = 3;
 8     srand(time(0));
 9     luckyDay = rand() % 30 + 1;
10 
11     printf("你有3次机会猜测11月的lucky day(1-30之间)。\n");
12 
13     while (attempts > 0) {
14         printf("请输入你猜测的日期:");
15         scanf("%d", &guess);
16 
17         if (guess < luckyDay) {
18             printf("你猜的日期早了。\n");
19         } else if (guess > luckyDay) {
20             printf("你猜的日期晚了。\n");
21         } else {
22             printf("恭喜你!你猜中了!\n");
23             return 0;
24         }
25 
26         attempts--;
27     }
28 
29     printf("很遗憾,你没有猜中。11月的lucky day是:%d\n", luckyDay);
30 
31     return 0;
32 }

 运行结果

 

posted @ 2024-10-11 21:39  yuanjialiang  阅读(9)  评论(0编辑  收藏  举报