实验二
任务一
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;//21 生成一个N1到N2之间的随机数 17 printf("20248329%04d\n", random_no); 18 } 19 else { 20 random_no = rand() % N3 + 1;//25 生成一个1到N3之间的随机数 21 printf("20248395%04d\n", random_no); 22 } 23 cnt++; 24 } 25 system("pause"); 26 return 0; 27 }//生成随机数,并通过运算输出5个学号
21
生成一个N1到N2之间的随机数并将值赋给random_no
25
生成一个1到N3之间的随机数并将值赋给random_no
生成随机数,并通过运算输出5个学号
任务二
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 }
任务三
1 #include<stdio.h> 2 3 int main(){ 4 char light; 5 while(scanf("%c",&light) !=EOF){ 6 getchar(); 7 if(light == 'r'){ 8 printf("stop!\n");} 9 if(light =='g'){ 10 printf("go go go\n");} 11 if(light == 'y'){ 12 printf("wait a minute\n");} 13 if( (light != 'r' && light !='g')&& light != 'y'){ 14 printf("something must be wrong...\n");} 15 } 16 return 0; 17 }
任务四
1 #include<stdio.h> 2 int main(){ 3 double a,max,min,sum; 4 max = 0; 5 min = 200000; 6 sum = 0; 7 while(scanf("%lf",&a) !=EOF){ 8 if(a == -1) 9 break; 10 if(a < min) 11 min = a; 12 if(a > max) 13 max = a; 14 sum += a; 15 } 16 printf("今日累计消费总额:%.1lf\n",sum); 17 printf("今日最高一笔开销:%.1lf\n",max); 18 printf("今日最低一笔开销:%.1lf\n",min); 19 return 0; 20 21 22 }
任务五
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 * a == b * b + c * c||b * b == a * a + c * c)||(c * c == a * a + b * b) ) 7 printf("直角三角形\n"); 8 else{ 9 if((a == b || a == c) || b == c) 10 if(a==b&&b==c) 11 printf("等边三角形\n"); 12 else 13 printf("等腰三角形\n"); 14 else 15 printf("普通三角形\n"); 16 }} 17 else 18 printf("不能构成三角形\n"); 19 } 20 return 0; 21 }
任务六
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 int main(){ 6 int day,a,t; 7 t = 0; 8 srand(time(NULL)); 9 day = (rand() % 30 + 1); 10 printf("猜猜2024年11月哪一天会是你的lucky day\n"); 11 printf("开始喽,你有三次机会,猜吧(1~30):"); 12 while(scanf("%d",&a)){ 13 if(t == 2){ 14 printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n",day); 15 break;} 16 if(a == day ) 17 printf("哇,猜中了:)\n"); 18 if(a > day){ 19 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 20 printf("再猜(1~30):");} 21 if(a < day){ 22 printf("你猜的日期早了,你的lucky day还没到呢\n"); 23 printf("再猜(1~30):");} 24 t++; 25 26 } 27 28 29 system("pause"); 30 return 0; 31 32 33 34 35 }