我现在需要写一段代码,由系统生成四个随机数abcd,有一个判断标准,在(100~150)是标准范围,现在判断这四个数是否在标准范围内,如果都比标准范围大那么输出1,都比标准范围小输出2,有大有小输出3,用c语言
时间: 2024-03-02 22:47:55 浏览: 14
好的,以下是您需要的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int a, b, c, d;
int standard_lower = 100; // 标准范围下限
int standard_upper = 150; // 标准范围上限
int count = 0; // 记录有几个数在标准范围内
// 生成随机数
srand(time(NULL)); // 设置随机数种子
a = rand() % 100 + 50; // 生成50~149的随机数
b = rand() % 100 + 50;
c = rand() % 100 + 50;
d = rand() % 100 + 50;
printf("随机数为:%d %d %d %d\n", a, b, c, d);
// 判断每个数是否在标准范围内
if (a >= standard_lower && a <= standard_upper) {
count++;
}
if (b >= standard_lower && b <= standard_upper) {
count++;
}
if (c >= standard_lower && c <= standard_upper) {
count++;
}
if (d >= standard_lower && d <= standard_upper) {
count++;
}
// 输出结果
if (count == 4) {
printf("都在标准范围内,输出1\n");
} else if (count == 0) {
printf("都不在标准范围内,输出2\n");
} else {
printf("有在标准范围内的数和不在标准范围内的数,输出3\n");
}
return 0;
}
```
这个程序会生成四个随机数,判断每个数是否在标准范围内,最后输出结果。注意,由于标准范围是100~150,所以生成的随机数需要加上50,才能在标准范围内。