实验2
test 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){ 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++; } system("pause"); return 0; }
问题1 确保生成的数字在397至476之间
问题2 确保生成的数字在1至21之间
问题3 随机生成5个学号
test2
#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=%.2gx2=%.2g\n",p1+p2,p1-p2); else{ printf("x1=%.2g+%.2gi,",p1,p2); printf("x2=%.2g+%.2gi\n",p1,p2); } } return 0; }
test3
#include <stdio.h> int main(){ char zimu; while(scanf("%c",&zimu)!=EOF){ if(zimu=='r'){ getchar(); printf("stop!\n"); } else if(zimu=='g'){ getchar(); printf("go go go\n"); } else if(zimu=='y'){ getchar(); printf("wait a minute\n"); } else{ getchar(); printf("something must be wrong\n"); } } return 0; }
test4
#include <stdio.h> int main() { float expense,min,max,total; total = 0, max = 0, min = 20000; printf("输入今日开销,直到输入-1终止:\n"); while (1) { scanf_s("%f", &expense); if (expense == -1) break; if (expense > max) max = expense; if (expense < min) min = expense; total += expense; } printf("今日累计消费总额:%.1f\n", total); printf("今日最高一笔开销:%.1f\n", max); printf("今日最低一笔开销:%.1f\n", min); return 0; }
test5
#include <stdio.h> int main() { int a, b, c; while (scanf_s("%d%d%d", &a, &b, &c) != EOF) { if (a + b <= c || a + c <= b || b + c <= a) printf("不能构成三角形\n"); else { if (a == b && a == c && b == c) printf("等边三角形\n"); else if (a == b || a == c || b == c) 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 printf("普通三角形\n"); } } return 0; }
test6
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int luckyday, guess, i=1; srand(time(NULL)); luckyday = rand() % 30 + 1; printf("猜猜2024年11月哪一天会是你的lucky day\n\n"); printf("开始咯,你有三次机会,猜吧(1~30):"); while(i<=3) { scanf_s("%d", &guess); printf("\n"); if (guess == luckyday) { printf("哇,猜中了:)"); break; } else if (guess < luckyday) printf("你猜的日期早了,你的luckyday还没到呢\n\n"); else printf("你的日期晚了,你的luckyday在前面哦\n\n"); i++; if(i<=3) printf("再猜(1~30):"); } if(i=4) printf("次数用光啦。偷偷告诉你,11月你的luckyday是%d号", luckyday); return 0; }