实验2
任务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) { printf("20248329%04d\n", random_no); } else { random_no = rand() % N3 + 1; printf("20248395%04d\n", random_no); } cnt++; } return 0; }
Line21:生成N1到N2之间的任意整数并赋值给random_no
Line25:生成1到N3间的任意整数并赋值给random_no
功能:在计类11.12班和奇安信班中随机抽取一个学号
任务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, x2 = %.2g\n", p1+p2, p1-p2); else { printf("x1 = %.2g + %.2gi, ", p1, p2); printf("x2 = %.2g - %.2gi\n", p1, p2); } } return 0; }
任务3:
#include<stdio.h> int main() { char ans; while(scanf("%c",&ans)!=EOF) { if(ans=='g') { printf("go go go\n"); } else if(ans=='y'){ printf("wait a minute\n"); } else if(ans=='r'){ printf("stop!\n"); } else{ printf("something must went wrong\n"); } getchar(); } return 0; }
任务4:
#include <stdio.h> int main(){ double a; double sum=0,max=0,min=20000; printf("输入今日开销,直到输入-1终止:\n"); while(scanf("%lf",&a)&&a!=-1){ sum=sum+a; if(a>max) max=a; if(a<min) min=a; } sum++; printf("今日累计消费总额:%.1lf\n",sum); printf("今日最高一笔开销:%.1lf\n",max); printf("今日最低一笔开销:%.1lf\n",min); return 0; }
任务5:
#include <stdio.h> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)!=EOF){ if((a+b)<c||(a+c)<b) printf("不能构成三角形\n"); else{ if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) printf("直角三角形\n"); else if(a==b&&a==c) printf("等边三角形\n"); else if(a==b||b==c||a==c) printf("等腰三角形\n"); else printf("普通三角形\n"); } } return 0; }
任务6:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int luckey_day,ans; int n=0; srand(time(NULL)); luckey_day=rand()%30+1; printf("猜猜2024年11月哪一天是你的luckey day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); while(scanf("%d",&ans)&&n!=2){ if(ans==luckey_day){ printf("猜中了!\n"); break;} else if(ans<luckey_day){ printf("你猜的日期早了,再猜:"); n++; continue;} else{ printf("你猜的日期晚了,再猜:"); n++; continue; } } if(n==2) printf("你的次数用光啦!偷偷告诉你,你11月的luckey day是%d号\n",luckey_day); return 0; }