作业二
任务一
#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) { random_no = rand() % (N2 - N1 + 1) + N1; printf("20248329%04d\n", random_no); } else { random_no = rand() % N3 + 1; printf("20248395%04d\n", random_no); } cnt++; } return 0; }
问题一
随机生成一个数范围为[397,476]
问题二
随机生成一个数范围为[1,21]
问题三
随机抽取学号
任务二
#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; }
任务三
#include <stdio.h> int main() { char a; while(scanf("%c",&a)!=EOF) { if(a=='r'){ printf("stop!\n"); } else if(a=='y'){ printf("wait a mintue\n"); } else if(a=='g'){ printf("go go go\n"); } else{ printf("something must be wrong\n"); } getchar(); } return 0; }
任务四
#include<stdio.h> int main (){ double tot,max,min,e,c; printf("输入今日开销,直至输入-1终止:\n"); tot=0; max=0; min=2000; while(scanf("%lf",&e)!=EOF){ tot=tot+e; getchar(); if (e==-1) break ; if (e>max) max=e; if (e<min) min=e; } printf("今日累计消费总额:%lf\n",tot); printf("今日最高一笔开销:%lf\n",max); printf("今日最低一笔开销:%lf\n",min); return 0; }
任务五
#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)||(b+c<=a)) {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)&&(b==c)){ printf("等边三角形\n"); } else if((a==b)||(b==c)||(c==a)){ printf("等腰三角形\n"); } else {printf("普通三角形\n"); } } } return 0; }
任务六
#include<stdlib.h> #include<stdio.h> #include<time.h> int main(){ srand(time(NULL)); int guess,cnt=0,luckyday; luckyday=rand()%30+1; printf("猜猜十一月哪一天会是你的lucy day,你有三次机会\n"); while (scanf("%d",&guess) !=EOF){ if(guess==luckyday){ printf("恭喜你猜中啦!"); return 0;} else if(cnt<2) { if(guess>luckyday){ printf("猜晚啦,再来一次吧!\n"); cnt+=1; } else if(guess<luckyday){ printf("猜早啦,再来一次吧!\n"); cnt+=1;} } else{ printf("次数用光喽!你的lucky day其实是%d",luckyday); return 0;} } return 0; }