task 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 }
问题一:
如果random_major生成的数是1,line21是生成一个在N1到N2之前的随机数,并赋值给random_no。
问题二:
如果random_major生成的数是0,line25是生成一个在N1和1之间的随机数,并赋值给random_no。
问题三:
这个程序的功能是在20248329或20248395后面在一定范围内随机生成四位数。
task 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 }
task 3:
1 #include<stdio.h> 2 int main(){ 3 char a; 4 5 while(scanf(" %c",&a)!=EOF){ 6 7 if(a == 'r') 8 printf("stop!\n"); 9 else if(a=='g') 10 printf("go go go\n"); 11 else if(a=='y') 12 printf("wait a minute\n"); 13 else 14 printf("something must be wrong\n"); 15 } 16 return 0; 17 }
task 4:
1 #include<stdio.h> 2 int main(){ 3 double a; 4 double max = 0.0; 5 double min = 20000.0; 6 double totala = 0.0; 7 printf("输入今天的开销,知道输入-1终止: "); 8 while(scanf(" %lf",& a)!=EOF&&a!=-1){ 9 if(a>max) 10 max = a; 11 else if (a<min) 12 min = a; 13 totala=totala+a;} 14 if(max>0&&min<20000&&totala>0){ 15 16 printf("今日最高一笔开销:%.1lf\n",max); 17 printf("今日最低一笔开销:%.1lf\n",min); 18 printf("今日累计消费总额:%.1lf\n",totala); 19 20 }else 21 printf("error.\n"); 22 23 24 return 0; 25 }
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||a+c<=b||b+c<=a){ 6 printf("不能构成三角形\n"); 7 }else{ 8 if(a==b&&b==c){ 9 printf("等边三角形\n"); 10 }else if(a==b||b==c||a==c){ 11 if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ 12 printf("等腰直角三角形\n"); 13 }else { 14 printf("等腰三角形\n"); 15 } 16 }else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ 17 printf("直角三角形\n"); 18 }else{ 19 printf("普通三角形\n"); 20 } 21 } 22 } 23 24 return 0; 25 26 }
task 6:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 int main() 6 { 7 printf("猜猜2024年11月哪一天会是你的lucky day\n\n"); 8 printf("开始喽,你有三次机会,猜吧(1~30):" ); 9 int t,sj,a; 10 t=0; 11 sj=rand()%31+1; 12 while(t!=3) 13 { 14 scanf("%d",&a); 15 if (a>sj) 16 { 17 printf("\n你猜的日期晚了,你的lucky day在前面哦\n"); 18 19 } 20 else if(a<sj) 21 { 22 printf("\n你猜的日期早了,你的lucky day还没到呢\n"); 23 24 } 25 else if(a==sj) 26 { 27 printf("哇,猜中了"); 28 break; 29 } 30 t++; 31 if(t<=2) 32 { 33 printf("\n再猜(1~30):"); 34 } 35 } 36 if(t==3) 37 { 38 printf("\n次数用光啦。偷偷告诉你,11月你的lucky day是%d号",sj); 39 40 } 41 }
标签:%.,int,random,else,实验,printf,include From: https://www.cnblogs.com/liujinyanyan/p/18456936