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