实验2
源代码1:
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 #define N1 397 #define N2 476 #define N3 21 int main(){ int cnt; int random_major,random_no; srand(time(NULL)); cnt=0; while(cnt<N){ random_major = rand()%2; if(random_major){ random_no = rand()%(N2-N1+1)+N1; printf("20248329%04d\n",random_no); } else{ random_no = rand() % N3+1; printf("20248395%04d\n",random_no); } cnt++; } return 0; }
截图1:
问题1:
1:随机生成N1至N2中的一个数
2:随机生成1至N3中的一个数
3:随机生成学号
源代码2:
1 #include<math.h> 2 3 int main(){ 4 double a,b,c; 5 double delta,p1,p2; //用于保存中间计算结果 6
7 while(scanf("%lf%lf%lf",&a,&b,&c) != EOF){ 8 if(a==0){ 9 printf("a = 0,invalid input\n"); 10 continue; 11 } 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 }
截图2:
源代码3:
#include<stdio.h> #include<stdlib.h> int main(){ char ans; while(scanf("%c",&ans)!=EOF){ while (getchar()!='\n'); if(ans =='r'){ printf("stop!\n"); } else if(ans == 'g'){ printf("go go go\n"); } else if(ans == 'y'){ printf("wait a minute\n"); } else{ printf("something must be wrong\n"); } } return 0; }
截图3:
源代码4:
#include<stdio.h> int main(){ double expense,min,max,total; expense =0; min=20000; max=0; total=0; while (scanf("%lf",&expense)!=EOF){ if(expense==-1) break; if(expense<min) min = expense; else if(expense>max) max = expense; total+=expense;} printf("今日累计消费总额:%.1lf\n",total); printf("今日最高一笔开销:%.1lf\n",max); printf("今日最低一笔开销: %.1lf\n",min); return 0; }
截图4:
源代码5:
#include<stdio.h> int main(){ int a,b,c; while(scanf("%d %d %d",&a,&b,&c)!=EOF) if(a+b<=c||a+c<=b||b+c<=a) printf("不能构成三角形\n"); else{ if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a)) printf("直角三角形\n"); else if(a==b&&b==c) printf("等边三角形\n"); else if(a==b||a==c||b==c) printf("等腰三角形\n"); else printf("普通三角形\n"); } return 0; }
截图5:
源代码6:
#include<stdio.h> #include<math.h> #include<time.h> int main(){ srand(time(NULL)); int n=0; int ans; int date = rand()%(5-1+1)+1; printf("猜猜2024年11月哪一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); while(scanf("%d", &date)!=EOF){ if(ans==date){ printf("哇,猜中了\n"); break; } if(ans>date){ printf("你猜的日期晚了,你的lucky day在前面哦\n"); n++; } else if(ans<date){ printf("你猜的日期早了,你的lucky day还没到呢\n"); n++; } if(n==3){ printf("次数用光啦,偷偷告诉你,11月你的lucky day是%d号\n",date); break; } } return 0; }
截图6: