实验二

源代码

#include #include #include #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;//21 将随机数除以(N2 - N1 +1)得到的余数加上 N1后得到的值赋给random_no printf("20248329%04d\n", random_no); } else { random_no = rand() % N3 + 1;//25 将随机数除以(N3)得到的余数加上 1后得到的值赋给random_no printf("20248395%04d\n", random_no); } cnt++; } system("pause"); return 0; }

结果

! 1.随机生成了一个397~476范围的数 2.随机生成了一个1~21范围的数 3.随机生成五个学号 

 

源代码

#include #include 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=%.ag/n",p1+p2,p1-p2); else { printf("x1=%.2g+%.2gi,",p1,p2); printf("x2=%.2g-%.2gi\n",p1,p2); } } getchar(); return 0; }

结果

源代码

#include int main() { char ace; while (scanf("%c", &ace) != EOF) { getchar(); if(ace=='r') printf("stop!\n"); if(ace=='g') printf("go go go\n"); if(ace=='y') printf("wait a minute\n"); if(ace!='r'&&ace!='g'&&ace!='y') printf("something must be wrong...\n"); } return 0; }

结果

源代码

 

#include int main(){ double a,max,min,sum; max = 0; min = 200000; sum = 0; while(scanf("%lf",&a) !=EOF){ if(a == -1) break; if(a < min) min = a; if(a > max) max = a; sum += a; } printf("今日累计消费总额:%.1lf\n",sum); printf("今日最高一笔开销:%.1lf\n",max); printf("今日最低一笔开销:%.1lf\n",min); return 0; }

结果

源代码

 

#include 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)) { if(a * a == b * b + c * c||b * b == a * a + c * c||c * c == a * a + b * b ) printf("直角三角形\n"); else if(a==b||b==c||a==c) { if(a==b&&b==c) printf("等边三角形\n"); else printf("等腰三角形\n"); } else printf("普通三角形\n"); } else printf("不能构成三角形\n"); } return 0; }

结果

源代码

 

#include #include int main() { int day,a,t; t = 0; srand(time(NULL)); day = (rand() % 30 + 1); printf("猜猜2024年11月哪一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); while(scanf("%d",&a)){ if(t == 2){ printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n",day); break;} if(a == day ) printf("哇,猜中了:)\n"); if(a > day){ printf("你猜的日期晚了,你的lucky day在前面哦\n"); printf("再猜(1~30):");} if(a < day){ printf("你猜的日期早了,你的lucky day还没到呢\n"); printf("再猜(1~30):");} t++; } system("pause"); return 0; }

结果

posted @ 2024-10-10 15:36  毛天佑202483290413  阅读(4)  评论(1编辑  收藏  举报