实验任务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 } 25 else{ 26 random_no=rand()%N3+1; 27 printf("20248395%04d\n",random_no); 28 29 } 30 cnt++; 31 } 32 return 0; 33 }
问题1:功能是生成N1~N2即397~476之间的随机数。
问题2:功能是生成1~N3即1~21之间的随机数。
问题3:随机抽取5个学号。
实验任务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 delta=b*b-4*a*c; 14 p1=-b/2/a; 15 p2=sqrt(fabs(delta))/2/a; 16 17 if(delta==0) 18 printf("x1=x2=%.2g\n",p1); 19 else if(delta>0) 20 printf("x1=%.2g,x2=%.2g\n",p1+p2,p1-p2); 21 else{ 22 printf("x1=%.2g+%.2gi,",p1,p2); 23 printf("x2=%.2g-%.2gi\n",p1,p2); 24 } 25 26 } 27 return 0; 28 }
实验任务3:
1 #include <stdio.h> 2 int main(){ 3 char color; 4 while(scanf("%c",&color)!=EOF){ 5 if(color=='r'){ 6 printf("stop!\n"); 7 getchar();} 8 else if(color=='g'){ 9 printf("go go go\n"); 10 getchar();} 11 else if(color=='y'){ 12 printf("wait a minute\n"); 13 getchar();} 14 else{ 15 printf("something must be wrong...\n"); 16 getchar();} 17 } 18 return 0; 19 20 }
实验任务4:
1 #include <stdio.h> 2 int main() 3 { 4 double s,max=0,min=20000; 5 double c=0; 6 printf("输入今日开销,直到输入-1终止:\n"); 7 8 while(1) 9 { 10 scanf("%lf",&s); 11 if(s==-1) 12 break; 13 else{ 14 if(s>max) 15 max=s; 16 if(s<max) 17 min=s; 18 } 19 20 c=c+s; 21 22 } 23 printf("今日累计消费总额:%.1f\n",c); 24 printf("今日最高一笔开销:%.1f\n",max); 25 printf("今日最低一笔开销:%.1f\n",min); 26 27 return 0; 28 }
实验任务5:
1 #include <stdio.h> 2 int main() 3 { 4 int a,b,c; 5 while(1){ 6 scanf("%d%d%d",&a,&b,&c); 7 if(a+b<=c||a+c<=b||b+c<=a) 8 printf("不能构成三角形\n"); 9 else{ 10 if(a==b&&b==c) 11 printf("等边三角形\n"); 12 else if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a)) 13 printf("等腰三角形\n"); 14 else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 15 printf("直角三角形\n"); 16 else 17 printf("普通三角形\n"); 18 19 } 20 } 21 return 0; 22 }
实验任务6:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 int luckyday,guess,i; 8 srand(time(NULL)); 9 luckyday=rand()%30+1; 10 printf("猜猜2024年11月哪一天会是你的luckyday\n"); 11 12 printf("\n开始喽,你有三次机会,猜吧(1~30):"); 13 14 for (i=0;i<3;i++){ 15 scanf("%d",&guess); 16 if(guess==luckyday){ 17 printf("\n哇,猜中了:)\n"); 18 return 0; 19 } 20 else if(guess<luckyday){ 21 if(i<2) { 22 printf("\n你猜的日期早了,你的luckyday还没到呢\n"); 23 printf("\n再猜(1~30):"); 24 } 25 else 26 printf("\n次数用光啦。偷偷告诉你,你的luckyday是%d号\n",luckyday); 27 28 } 29 else{ 30 if(i<2){ 31 printf("\n你猜的日期晚了,你的luckyday在前面哦\n"); 32 printf("\n再猜(1~30):"); 33 } 34 else 35 printf("\n次数用光啦。偷偷告诉你,你的luckyday是%d号\n",luckyday); 36 } 37 38 39 40 41 42 } 43 44 45 return 0; 46 }
标签:p2,p1,int,else,实验,printf,include From: https://www.cnblogs.com/yu-hong-yan/p/18455050