实验二
任务一源代码
#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; }
运行结果
问题1回答
生成一个在N1到N2范围内的随机数
问题2回答
生成一个在1到N3范围内的随机数
第三个问题回答
随机产生一个数字,且确保他在第一个范围或第二个范围里
任务二
源代码
#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 ch; while(scanf("%c", &ch) != EOF){ if(ch == 'y') { printf("wait a minute\n"); getchar(); } else if(ch == 'g'){ printf("go go go\n"); getchar(); } else if(ch == 'r') { printf("stop!\n"); getchar(); } else{ printf("something must be wrong...\n"); getchar(); } } return 0; }
运行结果
实验四
源代码
#include<stdio.h> int main(){ double expense; double totalexpense ; double maxexpense = 0.0; double minexpense = 100000000000.0; printf("请输入今天开销:"); scanf("%lf",&expense) ; while(expense != -1){ totalexpense = expense + expense; if(expense < minexpense){ minexpense = expense; } if(expense > maxexpense){ maxexpense = expense; } printf("请输入今天开销:"); scanf("%lf",&expense) ; } printf("今日累计消费总额:%.2lf\n",totalexpense); printf("今日最高一笔开销:%.2lf\n",maxexpense); printf("今日最低一笔开销:%.2lf\n",minexpense); return 0; }
运行结果
实验五
源代码
#include<stdio.h> int main(){ int a, b, c; printf("输入三角形三边边长(为整数):"); while(scanf("%d%d%d",&a, &b, &c)!= EOF){ if(a + b < c || b + c < a ||a + c < b) printf("三角形不存在\n"); else if(a == b && b == c) printf("三角形为等边三角形\n"); else if(a == b || b == c|| a == c) printf(" 三角形为等腰三角形\n"); else if(a*a + b*b == c*c|| b*b + c*c == a*a||a*a + c*c == b*b) printf("三角形为直角三角形\n"); else printf("三角形为普通三角形\n") ; printf("输入三角形三边边长(为整数):\n"); } return 0; }
运行结果
实验六
源代码
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int lurkday = rand() % 30 +1; int guess; int attempts = 3; srand(time(NULL)); printf("猜猜2024年11月的哪一天是你的lurk day。\n"); printf("开始咯,你有三次机会,猜吧(1~30):"); while(attempts > 0){ printf("你还有%d次机会。请输入你的猜测;",attempts); scanf("%d",&guess); if(guess == lurkday){ printf("哇,猜中了!\n"); return 0;} else if (guess < lurkday){ printf("你猜的日期早了,你的lurk day还没到呢,再猜。\n");} else{ printf("你猜的日期晚了,你的lurk day还在前面哦,再猜。\n");} attempts = attempts - 1; } printf("次数用光了。偷偷告诉你,11月你的lurk day是%d.\n",lurkday); return 0; }
运行结果
标签:%.,int,expense,else,实验,printf,include From: https://www.cnblogs.com/xl14/p/18454678