1、随机生成300个[1,100]的整数,将其转换为五级制(优、良、中、及格和不及格)的评定等级,然后统计各个等级人数语言代码
时间: 2024-05-13 21:15:17 浏览: 15
以下是Python的实现代码:
```python
import random
# 随机生成300个[1,100]的整数
numbers = [random.randint(1, 100) for _ in range(300)]
# 将数字转换为五级制评定等级
grades = []
for n in numbers:
if n >= 90:
grades.append("优")
elif n >= 80:
grades.append("良")
elif n >= 70:
grades.append("中")
elif n >= 60:
grades.append("及格")
else:
grades.append("不及格")
# 统计各个等级人数
grade_counts = {}
for g in grades:
if g in grade_counts:
grade_counts[g] += 1
else:
grade_counts[g] = 1
# 输出结果
for g, count in grade_counts.items():
print(g, count)
```
这段代码先随机生成了300个[1,100]的整数,然后将其转换为五级制评定等级。接着统计了各个等级人数,并输出结果。
相关问题
随机生成300个[1,100]的整数,将其转换为五级制(优、良、中、及格和不及格)的评定等级,然后统计各个等级人数。c语言代码
下面是符合您要求的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int grades[5] = {0}; // 五个评定等级的人数
int num, i;
srand(time(NULL)); // 设置随机数种子
for (i = 0; i < 300; i++) {
num = rand() % 100 + 1; // 生成1-100之间的随机数
if (num >= 90) {
grades[0]++; // 优秀
} else if (num >= 80) {
grades[1]++; // 良好
} else if (num >= 70) {
grades[2]++; // 中等
} else if (num >= 60) {
grades[3]++; // 及格
} else {
grades[4]++; // 不及格
}
}
// 输出各个等级的人数
printf("优秀:%d\n", grades[0]);
printf("良好:%d\n", grades[1]);
printf("中等:%d\n", grades[2]);
printf("及格:%d\n", grades[3]);
printf("不及格:%d\n", grades[4]);
return 0;
}
```
代码思路:先定义一个长度为5的数组来保存五个评定等级的人数,然后使用rand()函数生成300个1-100之间的随机整数,根据不同的数值分配评定等级,并将对应的等级人数加1。最后输出各个等级的人数即可。
用c语言写代码 1、 随机生成300个[1,100]的整数,将其转换为五级制(优、良、中、及格和不及格)的评定等级,然后统计各个等级人数。
好的,这是一道有趣的编程题目!以下是我给出的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int grades[5] = {0}; // 五个等级的人数初始化为0
srand(time(NULL)); // 用当前时间作为随机数生成器的种子
for (int i = 0; i < 300; i++) {
int num = rand() % 100 + 1; // 生成1-100之间的随机整数
if (num >= 90) {
grades[0]++;
} else if (num >= 80) {
grades[1]++;
} else if (num >= 70) {
grades[2]++;
} else if (num >= 60) {
grades[3]++;
} else {
grades[4]++;
}
}
// 输出各个等级的人数
printf("优:%d\n", grades[0]);
printf("良:%d\n", grades[1]);
printf("中:%d\n", grades[2]);
printf("及格:%d\n", grades[3]);
printf("不及格:%d\n", grades[4]);
return 0;
}
```
希望这份代码能够解决您的问题,如果您有其他问题,可以随时提出,我会尽力回答!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)