实验二
任务一
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 return 0; 32 33 }
1.生成397-476的随机数
2.生成1-21的随机数
3.以当前的时间作为随机数函数的种子,随机生成五个随机学号
任务二
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 }
任务三
1 #include<stdio.h> 2 int main(){ 3 char sig; 4 while(scanf("%c",&sig) !=EOF) 5 {getchar(); 6 7 if(sig=='y'){ 8 printf("wait a minute\n"); 9 } 10 else if(sig=='r'){ 11 printf("stop!\n"); 12 } 13 else if(sig=='g'){ 14 printf("go go go\n"); 15 } 16 else{ 17 printf("something must be wrong...\n"); 18 } 19 } 20 return 0; 21 }
任务四
1 #include<stdio.h> 2 int main(){ 3 double sum,max,min,data; 4 sum=0; 5 max=0; 6 min=20000; 7 while(scanf("%lf",&data)!=EOF){ 8 if(data==-1){ 9 break; 10 } 11 sum=sum+data; 12 if(data>max){ 13 max=data; 14 } 15 if(data<min){ 16 min=data; 17 } 18 } 19 printf("%.1f,%.1f,%.1f",max,min,sum); 20 return 0; 21 }
任务五
1 #include<stdio.h> 2 int main(){ 3 int a,b,c; 4 while(scanf("%d %d %d",&a,&b,&c)){ 5 if(a+b>c&&a+c>b&&b+c>a){ 6 if(a==b&&b==c){ 7 printf("等边三角形\n"); 8 } 9 else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ 10 printf("直角三角形\n"); 11 } 12 else if(a==b||a==c||b==c){ 13 printf("等腰三角形\n"); 14 } 15 else{ 16 printf("普通三角形\n"); 17 } 18 } 19 else{ 20 printf("不能构成三角形\n"); 21 } 22 23 } 24 }
任务六
1 #include <stdio.h> 2 int main(){ 3 int day,i,guess; 4 srand(time(NULL)); 5 day=rand()%30+1; 6 printf("猜猜lucky day\n你有三次机会:"); 7 for(i=0;i<3;i++){ 8 scanf("%d",&guess); 9 if(guess<day){ 10 printf("早了\n"); 11 printf("再猜:"); 12 } 13 else if(guess>day){ 14 printf("晚了\n"); 15 printf("再猜:"); 16 } 17 else{ 18 printf("猜中了:)"); 19 return 0; 20 } 21 } 22 printf("\n次数用光了,luckyday是%d号",day); 23 return 0; 24 }