实验2
实验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++; } return 0; }
line 21代码功能:产生一个随机数,范围在397-476之间
line25代码功能:产生一个随机数,范围在1-21之间
程序作用:输出学号,总共输出五次,若学号前缀为20248329,则随机397-476之间的一个值,补零后接在前缀后输出;若学号前缀为20248395,则随机1-21之间一个值,补零后输出
实验2
#include<stdio.h> #include<math.h> int main() { double a,b,c; double delta,p1,p2; while(scanf("%lf%lf%lf",&a,&b,&c)!=0) { if(a==0) { printf("a=0,invalid input"); 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; }
实验3
#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; }
实验4
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int main() { double min = 0.00; double max = 0.00; double a = 0.00; double cost; printf("输入今日开销,直至输入-1终止:\n"); while (scanf("%lf", &cost) != EOF) { if (cost == -1) { break; } a = a + cost; if (cost> max) { max = cost; } if (min==0) { min = cost; } if (cost< min) { min = cost; } } printf("今日累计消费总额:%.1lf", a); printf("今日最高一笔开销:%.1lf", max); printf("今日最低一笔开销:%.1lf", min); return 0; }
实验5
#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)) { 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 || c == a) { if (a == b && b == c && a == c) { printf("等边三角形\n"); } else { printf("等腰三角形\n"); } } else { printf("普通三角形\n"); } } else { printf("无法构成三角形\n"); } } return 0; }
实验6
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int a,b; int choice=3; srand(time(NULL)); a = rand()%30 +1; printf("猜猜2024年11月那一天会是你的幸运日\n开始喽,你只有三次机会,猜吧(1~30):"); while(choice > 0){ scanf("%d",&b) ; if(b == a){ printf("哇,猜中了:)"); return 0; } else if(b < a){ printf("你猜的日期早了\n再猜(1~30):"); } else if(b>a){ printf("你猜的日期晚了\n再猜(1~30):"); } choice--; } printf("次数用光啦,偷偷告诉你,11月你的幸运日是%d号",a); return 0; }