怎么C++编程:输入n个正整数,然后输出出现次数最多的那个数

来源:百度知道 编辑:UC知道 时间:2024/05/05 05:57:56
n是个未知数,是根据输入者按enter键结束
先谢谢各位大佬了,我被同学催急死了

用空格分隔输入的两个数,找出其中出现次数最多的数,如果有出现最多次数相同的元素则输出最先输入的那个数

#pragma warning(disable: 4786)
#include <iostream>
#include <sstream>
#include <string>
#include <map>

using namespace std;

int main()
{
map<string, int> x;
string temp(" ");
char line[1024];
cin.getline(line, 1024, '\n');
stringstream ss(line);
while(temp != "")
{
ss >> temp;
x[temp]++;
}
map<string, int>::iterator pos;
int max = 0;
for(map<string, int>::iterator it = x.begin(); it != x.end(); it++)
{
if(it->second > max)
{
pos = it;
max = it->second;
}
}
cout << pos->first << endl;
return 0;
}

指针指向一维数组,输入m个正整数,统计输出现最多的数,和出现次数
#include "stdio.h"
#include "conio.h"
int main()
{