c++实现描述 【背景描述】教堂在"新年许愿"活动,搜集了n名参与者的运行数字,想找出其中人气最高的幸运数字(0~10),帮助这些人实现愿望。 【问题描述】输入n个数字保存至数组中,找出其中出现次数最多的数字(已知出现最多的数字有一个),输出出现次数最多的数字以及其出现的次数。简便方法
时间: 2024-03-02 16:50:41 浏览: 37
可以使用一个长度为 11 的数组来统计每个数字出现的次数,下标为数字,值为出现次数。具体步骤如下:
1. 定义一个长度为 11 的数组 cnt,用于统计每个数字出现的次数。
2. 遍历输入的数组,对于每个数字,将其在 cnt 数组中对应的值加 1。
3. 遍历 cnt 数组,找到出现次数最多的数字及其出现次数,输出即可。
下面是具体的 C++ 代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int cnt[11] = {0}; // 定义数组,用于统计数字出现次数
int max_num = -1, max_cnt = -1; // 记录出现次数最多的数字和出现次数
for (int i = 0; i < n; i++) {
int num;
cin >> num;
cnt[num]++; // 更新数字出现次数
if (cnt[num] > max_cnt) { // 更新出现次数最多的数字和出现次数
max_cnt = cnt[num];
max_num = num;
}
}
cout << max_num << " " << max_cnt << endl; // 输出结果
return 0;
}
```
相关问题
c++实现描述 【背景描述】教堂在"新年许愿"活动,搜集了n名参与者的运行数字,想找出其中人气最高的幸运数字(0~10),帮助这些人实现愿望。 【问题描述】输入n个数字保存至数组中,找出其中出现次数最多的数字(已知出现最多的数字有一个),输出出现次数最多的数字以及其出现的次数。
可以使用 C++ 的 STL 库中的 map 或者 unordered_map 来实现。
具体步骤如下:
1. 定义一个 unordered_map<int, int> 统计每个数字出现的次数,其中键为数字,值为出现次数。
2. 遍历输入的数组,对于每个数字,如果在 map 中已经存在,则将其对应的值加 1,否则将其加入 map 中,并将其对应的值设为 1。
3. 遍历 map,找到出现次数最多的数字及其出现次数,输出即可。
下面是具体的 C++ 代码实现:
```c++
#include <iostream>
#include <unordered_map> // 引入 unordered_map 库
using namespace std;
int main() {
int n;
cin >> n;
unordered_map<int, int> cnt; // 定义 map,用于统计数字出现次数
int max_num = -1, max_cnt = -1; // 记录出现次数最多的数字和出现次数
for (int i = 0; i < n; i++) {
int num;
cin >> num;
cnt[num]++; // 更新数字出现次数
if (cnt[num] > max_cnt) { // 更新出现次数最多的数字和出现次数
max_cnt = cnt[num];
max_num = num;
}
}
cout << max_num << " " << max_cnt << endl; // 输出结果
return 0;
}
```
用C++实现描述 【背景描述】教堂在"新年许愿"活动,搜集了n名参与者的运行数字,想找出其中人气最高的幸运数字(0~10),帮助这些人实现愿望。 【问题描述】输入n个数字保存至数组中,找出其中出现次数最多的数字(已知出现最多的数字有一个),输出出现次数最多的数字以及其出现的次数。 输入描述 一行,一个正整数n ( 1≤n≤100 ) ; 一行,n个数字( 0≤数字≤10 ),数字之间使用空格分隔。 输出描述 一行,两个整数; 分别代表出现最多的数字和出现的次数;数字之间使用空格分隔。 用例输入 1 10 9 9 8 4 8 8 1 8 2 1 用例输出 1 8 4
以下是用 C++ 实现的代码:
```c++
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
cin >> n;
int num;
map<int, int> counts;
for (int i = 0; i < n; i++) {
cin >> num;
counts[num]++;
}
int max_num = 0;
int max_count = 0;
for (auto it = counts.begin(); it != counts.end(); it++) {
if (it->second > max_count) {
max_num = it->first;
max_count = it->second;
}
}
cout << max_num << " " << max_count << endl;
return 0;
}
```
解释:
首先读入参与者数量 `n`,然后用 `map` 来统计每个数字出现的次数。接着遍历 `map`,找出出现次数最多的数字和其出现的次数,最后输出即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)