如何编写程序,生成200个在[0,100]的随机数,统计不同数字出现的次数,并按不同数字出现的次数降序输出。
时间: 2024-05-01 11:22:36 浏览: 71
可以使用Python编写程序来实现这个功能,具体代码如下:
```python
import random
# 生成200个随机数
numbers = [random.randint(0, 100) for _ in range(200)]
# 统计不同数字出现的次数
count_dict = {}
for number in numbers:
if number not in count_dict:
count_dict[number] = 1
else:
count_dict[number] += 1
# 按照出现次数降序输出
count_list = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)
for number, count in count_list:
print(f"{number}: {count}")
```
首先,使用列表推导式生成200个随机数。然后,使用一个字典来统计不同数字出现的次数,遍历随机数列表,如果该数字不在字典中则添加到字典中,并将出现次数初始化为1;否则出现次数加1。最后,使用`sorted()`函数和lambda表达式对字典进行排序,并按照降序输出每个数字和出现次数。
相关问题
编写程序,生成一个列表,包含25个60-79之间的随机数,并统计列表中有几个不一样的数字及其出现的次数
你可以使用Python内置的random模块来生成随机数。以下是一个可能的实现:
```python
import random
random_list = [random.randint(60, 79) for _ in range(25)]
print("随机数列表:", random_list)
unique_nums = set(random_list)
num_count = {num: random_list.count(num) for num in unique_nums}
print("不同数字数量:", len(unique_nums))
print("每个数字出现的次数:", num_count)
```
首先,我们使用列表推导式生成一个包含25个60-79之间随机数的列表。然后,我们使用set函数获取列表中不同的数字,并使用字典推导式统计每个数字在列表中出现的次数。
输出结果类似如下:
```
随机数列表: [75, 69, 67, 65, 66, 60, 71, 72, 75, 65, 71, 72, 78, 66, 68, 72, 63, 67, 71, 62, 76, 71, 69, 78, 73]
不同数字数量: 15
每个数字出现的次数: {65: 2, 66: 2, 67: 2, 68: 1, 69: 2, 71: 4, 72: 4, 73: 1, 75: 2, 76: 1, 78: 2, 60: 1, 62: 1, 63: 1}
```
输出结果中,不同数字数量为15,即列表中有15个不同的数字;每个数字出现的次数也被正确地统计出来。
用C语言显示0-9之间的1000个真随机数,统计每个数字出现的次数并显示,并显示出出现次数最多的数字
在C语言中,你可以使用`rand()`函数生成0-9之间的随机数,并配合数组和循环结构来统计每个数字出现的次数。以下是一个简单的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COUNTS 10 // 数字范围
#define MAX_COUNTS 1000 // 需要生成的随机数数量
int counts[COUNTS] = {0}; // 初始化计数数组
// 生成随机数并更新计数
void generate_random_and_count() {
int random_num;
srand(time(0)); // 设置随机种子,每次运行得到不同的序列
for (int i = 0; i < MAX_COUNTS; ++i) {
random_num = rand() % COUNTS; // 0到9的随机数
counts[random_num]++;
}
}
// 显示统计结果
void display_counts() {
printf("Number\tCount\n");
for (int i = 0; i < COUNTS; ++i) {
printf("%d\t%d\n", i, counts[i]);
}
// 找出出现次数最多的数字
int max_count = 0, max_number;
for (int i = 0; i < COUNTS; ++i) {
if (counts[i] > max_count) {
max_count = counts[i];
max_number = i;
}
}
printf("\nMost frequent number: %d, appears %d times\n", max_number, max_count);
}
int main() {
generate_random_and_count();
display_counts();
return 0;
}
```
这个程序首先初始化一个计数数组,然后通过循环生成随机数并累加对应的计数。最后,它会打印每个数字及其出现次数,以及出现次数最多的数字。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)