任务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 输入397到476之间的数
2输入1到21
3选5个
任务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 }
任务3:
1 #include<stdio.h> 2 int main(){ 3 char a; 4 while(scanf("%c",&a)!=EOF){ 5 if(a=='r'){ 6 printf("stop!\n"); 7 getchar(); 8 } 9 else if(a=='g'){ 10 printf("go go go\n"); 11 getchar(); 12 } 13 else if(a=='y'){ 14 printf("wait a minute\n"); 15 getchar(); 16 } 17 else{ 18 printf("something must be wrong...\n"); 19 getchar(); 20 } 21 } 22 return 0; 23 24 25 26 }
任务4:
1 #include<stdio.h> 2 int main(){ 3 double a,t=0,max,min; 4 max=a; 5 min=a; 6 printf("输入今日开销,直到输入-1终止:\n"); 7 scanf("%lf",&a); 8 while(a>0){ 9 t+=a; 10 if(a>max) 11 max=a; 12 else 13 min=a; 14 15 scanf("%lf",&a); 16 17 } 18 19 20 printf("今日累计消费总额:%.1lf\n",t); 21 printf("今日最高一笔开销:%.1lf\n",max); 22 printf("今日最低一笔消费:%.1lf\n",min); 23 }
任务5
1 #include <stdio.h> 2 3 int main() { 4 int a,b,c; 5 while(1){ 6 scanf("%d%d%d",&a,&b,&c); 7 getchar(); 8 if((a+b<=c)||(a+c<=b)||(b+c<=a)){ 9 printf("不能构成三角形\n"); 10 11 } 12 else if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a)){ 13 printf("构成直角三角形\n"); 14 } 15 else if((a==b)&&(a==c)){ 16 printf("构成等边三角形\n"); 17 } 18 else if(a==b||b==c||a==c){ 19 printf("构成等腰三角形\n"); 20 } 21 else{ 22 printf("构成普通三角形\n"); 23 } 24 } 25 return 0; 26 }
任务6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 int main(){ 5 6 int random,a,b,c,n; 7 srand(time(NULL)); 8 printf("猜猜2024年11月哪一天会是你的lucky day\n"); 9 n=0; 10 random=rand()%30+1; 11 printf("开始喽,你有三次机会,猜吧(1~30):"); 12 while(n!=3){ 13 scanf("%d",&a); 14 if(a<random) 15 {printf("你猜的日期早了,你的lucky day还没到呢\n"); 16 } 17 else if(a>random){ 18 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 19 20 } 21 else if(a==random){ 22 printf("哇,猜中了:)"); 23 break; 24 } 25 26 27 n=n+1; 28 getchar(); 29 if(n<=2){ 30 printf("再猜(1~30):"); 31 } 32 if(n==3){ 33 printf("次数用光啦,偷偷告诉你,11月你的lucky day是%d号",random); 34 } 35 } 36 return 0; 37 }
标签:%.,int,random,else,实验,printf,include From: https://www.cnblogs.com/zhy123ZHY/p/18462436