实验1
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 5 6 #define N1 397 7 #define N2 476 8 #define N3 21 9 10 int main() { 11 int cnt; 12 int random_major, random_no; 13 14 srand(time(NULL)); 15 16 cnt = 0; 17 while(cnt < N) { 18 random_major = rand() % 2; 19 20 if(random_major) { 21 random_no = rand() % (N2 - N1 + 1) + N1; 22 printf("20248329%04d\n", random_no); 23 } 24 else { 25 random_no = rand() % N3 + 1; 26 printf("20248395%04d\n", random_no); 27 } 28 29 cnt++; 30 } 31 32 return 0; 33 34 }
图片
问题
生成397到476的随机数
生成0到21的随机数
抽取随机学号
实验2
源代码
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 double a, b, c; 6 double delta, p1, p2; 7 8 while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { 9 if(a == 0) { 10 printf("a = 0, invalid input\n"); 11 continue; 12 } 13 14 delta = b*b - 4*a*c; 15 p1 = -b/2/a; 16 p2 = sqrt(fabs(delta))/2/a; 17 if(delta == 0) 18 printf("x1 = x2 = %.2g\n", p1); 19 else if(delta > 0) 20 printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2); 21 else { 22 printf("x1 = %.2g + %.2gi, ", p1, p2); 23 printf("x2 = %.2g - %.2gi\n", p1, p2); 24 } 25 } 26 27 return 0; 28 }
图片
实验3
源代码
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 char c; 6 while(scanf("%c", &c) != EOF){ 7 getchar(); 8 if(c=='r') 9 printf("stop!\n"); 10 else if(c=='g') 11 printf("go go go\n"); 12 else if(c=='y') 13 printf("wait a minute\n"); 14 else 15 printf("something must be wrong...\n"); 16 } 17 return 0; 18 }
图片
实验4
源代码
1 include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 double x, max, min, s = 0; 6 scanf("%lf", &x); 7 max = x; 8 min = x; 9 while(x>=0){ 10 s = s + x; 11 if(x > max) 12 max = x; 13 if(x < min) 14 min = x; 15 scanf("%lf", &x); 16 if(x != -1) 17 continue; 18 19 } 20 printf("今日累计消费总额:% .1lf\n今日最高一笔开销:% .1lf\n今日最低一笔开销:% .1lf\n",s ,max, min); 21 return 0; 22 }
图片
实验5
源代码
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 int a, b, c; 6 while(scanf("%d%d%d",&a, &b, &c) != EOF){ 7 if((a + b <= c) ||(a - b >= c)) 8 printf("不能构成三角形\n"); 9 else{ 10 if(a == b&& b== c) 11 printf("等边三角形\n"); 12 else if((a == b)&&(b != c)||(a == c)&&(c !=b)||(b == c)&&(b != a)) 13 printf("等腰三角形\n"); 14 else if((a^2 + b^2 == c^2)||(a^2 + c^2 == b^2)||(b^2 + c^2 == a^2)) 15 printf("直角三角形\n"); 16 else 17 printf("普通三角形\n"); 18 } 19 } 20 return 0; 21 }
图片
实验6
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 srand(time(NULL)); 7 int random_no , n ,a; 8 n = 0; 9 random_no = rand()%30 + 1; 10 printf("猜猜2024年11月哪一天会是你的luckey day\n\n"); 11 printf("开始喽,你有三次机会,猜吧(1~30):", a); 12 while(n < 3){ 13 scanf("%d", &a); 14 if(a == random_no){ 15 printf("\n哇,猜中了:)"); 16 break; 17 } 18 else if(a > random_no){ 19 printf("\n你猜的日期晚了,你的luckey day在前面哦\n\n"); 20 } 21 else { 22 printf("\n你猜的日期早了,你的luckey day在后面哦\n\n"); 23 } 24 n++; 25 if(n <= 2) 26 printf("再猜(1~30):"); 27 } 28 if(n == 3){ 29 printf("次数用光了,偷偷告诉你,11月你的luckey day是%d号\n", random_no); 30 } 31 32 return 0; 33 }
图片
标签:no,int,random,else,实验,printf,include From: https://www.cnblogs.com/schince/p/18456559