任务一
#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之间的随机数作为学号后缀
本程序功能为首先根据系统时间生成随机数种子,再生成随机数,若为奇数,则为20248329前缀的学号随机生成397到476之间的后缀,接着补0输出,若为偶数,则为20248395前缀的学号生成1到21之间的后缀,补0输出,上述流程共重复5次,共打印五行学号
任务二
#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("%s",&a)!=EOF){ if(a=='y'){ printf("wait a minute\n"); } else if(a=='r'){ printf("stop!\n"); } else if(a=='g'){ printf("go go go\n"); } else{ printf("something must be wrong...\n"); } } return 0; }
任务四
#include <stdio.h> int main(){ printf("输入今日开销,直到输入-1终止\n"); double money,sum=0,maxn=0,minn=1e10; while(scanf("%lf",&money) && (money!=-1)){ if(money>maxn){ maxn=money; } if(money<minn){ minn=money; } sum+=money; } printf("今日累计消费总额:%.1f\n",sum); printf("今日最高一笔开销:%.1f\n",maxn); printf("今日最低一笔开销:%.1f\n",minn); return 0; }
任务五
#include <stdio.h> int main() { int a, b, c; while (scanf("%d %d %d", &a, &b, &c) == 3) { if (a + b <= c || a + c <= b || b + c <= a) { printf("不能构成三角形\n"); } else { if (a == b && b == c) { printf("等边三角形\n"); } else if (a == b || a == c || b == c) { printf("等腰三角形\n"); } else { int max_side = a; if (b > max_side) max_side = b; if (c > max_side) max_side = c; if (max_side == a) { if (b * b + c * c == a * a) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } else if (max_side == b) { if (a * a + c * c == b * b) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } else { if (a * a + b * b == c * c) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } } } } return 0; }
任务六
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int lucky_day, guess; int attempts = 3; srand(time(NULL)); lucky_day = rand() % 30 + 1; printf("欢迎来到猜日期游戏!请猜测11月的幸运日(1到30):\n"); while (attempts > 0) { printf("你还有 %d 次机会。请输入你的猜测: ", attempts); scanf("%d", &guess); if (guess < 1 || guess > 30) { printf("请输入一个有效的日期(1到30之间)!\n"); continue; } if (guess == lucky_day) { printf("恭喜你,猜对了!\n"); return 0; } else if (guess < lucky_day) { printf("你猜的日期太早了。\n"); } else { printf("你猜的日期太晚了。\n"); } attempts--; } printf("很遗憾,你没有猜中。11月的幸运日是 %d。\n", lucky_day); return 0; }
标签:random,C语言,int,编程,else,%.,printf,include,分支 From: https://www.cnblogs.com/wangzhadan/p/18452297