实验2
任务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 cnt++; 29 } 30 return 0; 31 }
从N1到N2中随机取一个数
从N3中取一个数
从2个班中取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 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 }
任务3:
1 #include <stdio.h> 2 int main(){ 3 while(1){ 4 char input; 5 scanf("%c",&input); 6 if(input=='r'){ 7 printf("stop!\n"); 8 getchar(); 9 } 10 else if(input=='g'){ 11 printf("go go go\n"); 12 getchar(); 13 } 14 else if(input=='y'){ 15 printf("wait a minute\n"); 16 getchar(); 17 } 18 else{ 19 printf("something must be wrong...\n"); 20 getchar(); 21 } 22 } 23 return 0; 24 }
任务4:
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(){ 4 double a; 5 double b; 6 double c; 7 double sum; 8 c=20000; 9 b=0; 10 sum=0; 11 a=0; 12 printf("输入今日开销,直到输入-1终止:\n"); 13 14 while(a!=-1){ 15 scanf("%lf",&a); 16 17 getchar(); 18 19 if(a>b){ 20 b=a; 21 } 22 if(a>=0){ 23 24 if(a<c){ 25 c=a; 26 } 27 } 28 29 sum=sum+a; 30 } 31 printf("今日累计消费总额:%.1lf\n",sum); 32 printf("今日最高一笔开销:%.1lf\n",b); 33 printf("今日最低一笔开销:%.1lf\n",c); 34 35 36 37 38 return 0; 39 }
任务5:
1 #include <stdio.h> 2 3 4 int main(){ 5 int a,b,c; 6 7 8 while(1){ 9 scanf("%d%d%d",&a,&b,&c); 10 getchar(); 11 if((a+b<=c)||(a+c<=b)||(b+c<=a)){ 12 printf("不能构成三角形\n"); 13 14 } 15 else if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a)){ 16 printf("构成直角三角形\n"); 17 18 } 19 else if((a==b)&&(a==c)){ 20 printf("构成等边三角形\n"); 21 } 22 else if(a==b||b==c||a==c){ 23 printf("构成等腰三角形\n"); 24 } 25 else{ 26 printf("构成普通三角形\n"); 27 } 28 } 29 return 0; 30 }
任务6:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <time.h> 4 5 6 7 int main(){ 8 int random,a,b,c,n;3 9 srand(time(NULL)); 10 printf("猜猜2024年11月哪一天会是你的lucky day\n"); 11 n=0; 12 random=rand()%30+1; 13 printf("开始喽,你有三次机会,猜吧(1~30):"); 14 while(n!=3){ 15 scanf("%d",&a); 16 if(a<random) 17 {printf("你猜的日期早了,你的lucky day还没到呢\n"); 18 } 19 else if(a>random){ 20 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 21 22 } 23 else if(a==random){ 24 printf("哇,猜中了:)"); 25 break; 26 } 27 28 29 n=n+1; 30 getchar(); 31 if(n<=2){ 32 printf("再猜(1~30):"); 33 } 34 if(n==3){ 35 printf("次数用光啦,偷偷告诉你,11月你的lucky day是%d号",random); 36 } 37 } 38 return 0; 39 }