task_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;//随机生成从397到476的一个值 printf("20248329%04d\n", random_no); } else { random_no = rand() % N3 + 1; printf("20248395%04d\n", random_no);//随机生成1到21的一个值 } cnt++; } return 0; }
第二十一行的作用是随机生成397到476的一个值
第二十五行的作用是随机生成1到21的一个值
整个程序的作用是随机抽取名同学的学号
task_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)!=EOF) { if(a == 0) { printf("a=o,不是一元二次方程"); 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; }
task_3
#include<stdio.h> int main() { char x; printf("请输入信号灯的颜色,Y、y代表黄灯,R、r代表红灯,G,g代表绿灯\n"); while(scanf(" %c",&x)!=EOF) { if(x == 'r'||x == 'R') { printf("stop\n"); } else if(x == 'y'||x == 'Y') { printf("wait a minute\n"); } else if(x == 'g'||x == 'G') { printf("go\n"); } else { printf("wrong color\n"); } } return 0; }
task_4
#include <stdio.h> int main() { double shuru, sum = 0.0, max = 0.0, min = 20001.0; printf("输入今日开销,直到输入-1终止:\n"); while (1) { scanf("%lf", &shuru); if (shuru == -1) break; else { sum += shuru; max = max < shuru ? shuru : max; min = min < shuru ? min : shuru; } } printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf", sum, max, min); return 0; }
task_5
#include<stdio.h> #include<math.h> int main() { int i;//循环变量 int a, b, c;//三角形的三边 printf_s("输入三角形的三边长\n"); for (i = 0;i <= i + 1; ++i) { scanf_s("%d %d %d", &a, &b, &c); if (a + b <= c || b + c <= a || c + a <= b) { printf_s("不构成三角形\n"); } else if (a == b && b == c && c == a) { printf_s("等边三角形\n"); } else if (a == b && a != c || b == c && b != a || c == a && c != b) { printf("等腰三角形\n"); } else if (pow(a, 2) + pow(b, 2) == pow(c, 2) || pow(b, 2) + pow(c, 2) == pow(a, 2) || pow(c, 2) + pow(a, 2) == pow(b, 2)) { printf_s("直角三角形\n"); } else { printf_s("普通三角形\n"); } } return 0; }
task_6
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); int lucky_day = rand() % 30 + 1; int a = 0;
int i; while (a < 3)
{ scanf("%d", &i); if (i == lucky_day) { printf("猜中了\n"); a = 3; return 0; } else if (i > lucky_day) { printf(" 猜晚了\n"); ++a; } else { printf("猜早了\n"); ++a; } } printf("%d\n", lucky_day); return 0; }
标签:%.,int,random,else,实验,printf,include From: https://www.cnblogs.com/jiaoaodedihuazhixiu/p/18459176