输入一组数字 输出其中出现次数最多的数字并显示出出现次数C++
时间: 2023-10-30 20:06:15 浏览: 72
我是AI语言模型,我可以为您提供代码示例,但需要您提供具体的输入数据格式和输出要求。
以下是一种可能的实现,假设输入的数字序列以空格分隔:
```C++
#include <iostream>
#include <unordered_map>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string input;
getline(cin, input); // 读入一行输入
unordered_map<int, int> count; // 记录每个数字出现的次数
stringstream ss(input);
int num;
while (ss >> num) {
count[num]++;
}
int max_count = 0, max_num = 0;
for (auto& p : count) {
if (p.second > max_count) {
max_count = p.second;
max_num = p.first;
}
}
cout << "出现次数最多的数字是 " << max_num << ",出现了 " << max_count << " 次。" << endl;
return 0;
}
```
例如,如果输入是 "1 2 2 3 3 3 4 4 4 4",则输出为:
```
出现次数最多的数字是 4,出现了 4 次。
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)