实验二
task1
Q1:赋值random_no班级内学号的随机值
Q2:赋值random_no班级内奇安信班学号的随机值
Q3:随机抽取班级内同学的学号
task2
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 }
task3
1 #include<stdio.h> 2 int main() 3 { 4 char read; 5 while(scanf("%c",&read) != EOF) 6 { 7 getchar(); 8 if (read == 'r') 9 { 10 printf("stop!\n"); 11 } 12 else if (read == 'g') 13 { 14 printf("go go go\n"); 15 } 16 else if (read == 'y') 17 { 18 printf("wait a minute\n"); 19 } 20 else 21 { 22 printf("somthing must be wrong\n"); 23 } 24 } 25 26 return 0; 27 }
task4
1 #include<stdio.h> 2 #include<math.h> 3 int main() 4 { 5 printf("输入今日开销,直到输入-1终止:\n"); 6 double read,max = 0,min = 0; 7 double sum = 0; 8 do 9 { 10 11 scanf("%lf",&read); 12 sum += read; 13 if(read == -1) 14 { 15 break; 16 } 17 else if(read >= max) 18 max = read; 19 else 20 min = read; 21 }while( read > 0 && read <= 20000); 22 printf("今日累计消费总额:%.1lf\n",sum); 23 printf("今日最高一笔开销:%.1lf\n",max); 24 printf("今日最低一笔开销:%.1lf\n",min); 25 return 0; 26 }
task5
1 #include<stdio.h> 2 #include<math.h> 3 int main() 4 { 5 int a, b, c; 6 while(scanf("%d%d%d",&a,&b,&c)!=EOF) 7 { 8 if(a+b<=c||a+c<=b||b+c<=a) 9 { 10 printf("不能构成三角形\n"); 11 } 12 else if((a==b)&&(a==c)&&(b==c)) 13 { 14 printf("等边三角形\n"); 15 } 16 else if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a)) 17 { 18 printf("等腰三角形\n"); 19 } 20 else if(pow(a, 2) + pow(b, 2) == pow(c, 2)||pow(a, 2) + pow(c, 2) == pow(b, 2)||pow(c, 2) + pow(b, 2) == pow(a, 2)) 21 { 22 printf("直角三角形\n"); 23 } 24 else 25 printf("普通三角形\n"); 26 } 27 return 0; 28 }
task6
1 #include<stdio.h> 2 int main() 3 { 4 int ld; 5 int gue; 6 int i ; 7 srand(time(0)); 8 ld = rand()%30 + 1; 9 printf("猜猜2024年11月哪一天会是你的lucky day\n"); 10 printf("开始喽,你有三次机会,猜吧(1~30):"); 11 while(i<2) 12 { 13 i++; 14 scanf("%d",&gue); 15 if(gue>ld) 16 { 17 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 18 printf("再猜(1~30):"); 19 } 20 else if(gue<ld) 21 { 22 printf("你猜的日期早了,你的lucky day还没到呢\n"); 23 printf("再猜(1~30):"); 24 } 25 else 26 { 27 printf("哇,猜中了:)"); 28 break; 29 } 30 } 31 scanf("%d",&gue); 32 if(gue>ld) 33 { 34 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 35 } 36 else if(gue<ld) 37 { 38 printf("你猜的日期早了,你的lucky day还没到呢\n"); 39 } 40 else 41 printf("哇,猜中了:)"); 42 if(gue!=ld) 43 printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号",ld); 44 45 return 0; 46 }