实验2
task.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:让取值在398到476之间
问题2:让取值在1到21之间
问题3:在奇安信班和计算机班随机取五个值
task.2
程序:
#include <stdio.h> #include <math.h> int main(){ double a,b,c; double delta,p1,p2; while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){ if(a==0){ printf("a=0,invalid input\n"); continue; } delta=b*b-4*a*c; p1=-b/2/a; p2=sqrt(fabs(delta))/2/a; if(delta==0) printf("x1=x2=%.2g\n",p1); else if(delta>0) printf("x1=%.2g+%.2g\n",p1+p2,p1-p2); else{ printf("x1=%.2g+%.2gi,",p1,p2); printf("x2=%.2g-%.2gi\n",p1,p2); } } return 0; }
结果:
task.3
程序:
#include <stdio.h> int main(){ char traffic; while(scanf("%c",&traffic)!=EOF){ getchar(); if(traffic=='y'){ printf("wait a minute\n"); } else if(traffic=='r'){ printf("stop!\n"); } else if(traffic=='g'){ printf("go go go\n"); } else{ printf("something must be wrong...\n"); } } return 0; }
结果:
task.4
程序:
#include <stdio.h> int main(){ double x,max,min,s=0; scanf("%lf",&x); max=x; min=x; while(x<20000&&x>0){ s=s+x; if(x>max)max=x; if(x<min)min=x; scanf("%lf",&x); } printf("今日累计消费总额:%.1f\n",s); printf("今日最高一笔开销:%.1f\n",max); printf("今日最低一笔开销:%.1f\n",min); return 0; }
结果:
task.5
程序:
#include <stdio.h> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ if(a+b<=c||b+c<=a||a+c<=b){printf("不能构成三角形");continue;} if(a==b&&b==c)printf("构成等边三角形"); if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ printf("构成直角三角形");continue;} if(a!=b&&a!=c&&b!=c)printf("构成普通三角形"); if((a==b&&a!=c)||(b==c&&c!=a)||(a==c&&a!=b))printf("构成等腰三角形"); } return 0; }
结果:
task.6
程序:
#include <stdio.h> #include <math.h> #define N 1 #define N2 30 int main(){ int x,y,z=0; srand(time(NULL)); y=rand()%(N2-N+1)+1; printf("猜猜2024年11月哪一天会是你的lucky day\n"); printf("\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); while(scanf("%d",&x)!=EOF){ printf("\n"); ++z; if(x==y){printf("哇,猜中了");break;} if(x<y){printf("你猜的日期早了,你的lucky day还没到呢\n");printf("\n"); if(z>=3){printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号",y);break;} if(z>=3)continue; else printf("再猜(1~30):");} if(x>y){printf("你猜的日期晚了,你的lucky day在前面哦\n");printf("\n"); if(z>=3){printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号",y);break;} if(z>=3)continue; else printf("再猜(1~30):");} } return 0; }
结果: