任务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 }
运行结果:
问题1:解释line21代码 random_no = rand() % (N2 - N1 + 1) + N1; 的功能
答:生成[397, 476]内的随机整数
问题2:解释line25代码 random_no = rand() % N3 + 1; 的功能
答:生成[1, 21]内的随机整数
问题3:这个程序的功能是什么?
答:从后四位为1~21,397~476的学号中随机抽取五个学号,每次抽取时学号来自两个班的概率相等
任务2
源代码:
1 // 一元二次方程求解 2 3 #include <stdio.h> 4 #include <math.h> 5 6 int main() { 7 double a, b, c; 8 double delta, p1, p2; // 用于保存中间计算结果 9 10 while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { 11 if(a == 0) { 12 printf("a = 0, invalid input\n"); 13 continue; 14 } 15 16 delta = b*b - 4*a*c; 17 p1 = -b/2/a; 18 p2 = sqrt(fabs(delta))/2/a; 19 20 if(delta == 0) 21 printf("x1 = x2 = %.2g\n", p1); 22 else if(delta > 0) 23 printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2); 24 else { 25 printf("x1 = %.2g + %.2gi, ", p1, p2); 26 printf("x2 = %.2g - %.2gi\n", p1, p2); 27 } 28 } 29 30 return 0; 31 }
运行结果:
任务3:
源代码:
1 #include <stdio.h> 2 3 int main(){ 4 5 char c; 6 7 while(scanf("%c",&c) != EOF) 8 { 9 getchar(); 10 if(c == 'r') 11 printf("stop!\n"); 12 else if(c == 'g') 13 printf("go go go\n"); 14 else if(c == 'y') 15 printf("wait a minute\n"); 16 else 17 printf("something must be wrong...\n"); 18 } 19 20 return 0; 21 }
运行结果:
任务4:
源代码:
1 #include <stdio.h> 2 3 int main() 4 { 5 double shuru, sum = 0.0, max = 0.0, min = 20001.0; 6 7 printf("输入今日开销,直到输入-1终止:\n"); 8 9 while (1) 10 { 11 scanf("%lf", &shuru); 12 if (shuru == -1) 13 break; 14 else 15 { 16 sum += shuru; 17 max = max < shuru ? shuru : max; 18 min = min < shuru ? min : shuru; 19 } 20 } 21 22 printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf", sum, max, min); 23 24 return 0; 25 }
运行结果:
任务5
源代码:
1 #include <stdio.h> 2 3 int main() 4 { 5 int a, b, c; 6 7 while (scanf("%d%d%d", &a, &b, &c) != EOF) 8 { 9 if (a + b > c && a + c > b && b + c > a) 10 { 11 if (a == b && b == c) 12 printf("等边三角形\n"); 13 else if (a == b || b == c || a == c) 14 printf("等腰三角形\n"); 15 else if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b) 16 printf("直角三角形\n"); 17 else 18 printf("普通三角形\n"); 19 } 20 else 21 { 22 printf("不能构成三角形\n"); 23 } 24 } 25 26 return 0; 27 }
运行结果:
任务6
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 int lucky_day, shuru; 8 9 srand((unsigned)time(NULL)); 10 lucky_day = rand() % 30 + 1; 11 12 printf("猜猜2024年11月哪一天会是你的lucky day\n\n开始喽,你有三次机会,猜吧(1~30):"); 13 14 int i; 15 16 for (i = 3; i >= 1; i--) 17 { 18 scanf("%d", &shuru); 19 if (shuru == lucky_day) 20 { 21 printf("\n哇,猜中了:)"); 22 return 0; 23 } 24 else if (shuru < lucky_day) 25 printf("\n你猜的日期早了,你的lucky day还没到呢\n\n"); 26 else if (shuru > lucky_day) 27 printf("\n你猜的日期晚了,你的lucky day在前面哦\n\n"); 28 if (i != 1) 29 printf("再猜(1~30):"); 30 } 31 32 printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号", lucky_day); 33 34 return 0; 35 }
运行结果:
标签:%.,int,else,shuru,实验,printf,include From: https://www.cnblogs.com/yucyi/p/18454689