您的位置:首页 > 其它

2016华为上机题:挑选出现最多的数字

2016-09-21 18:30 232 查看
问题描述:

输入一串整型数据,取出整形数据中出现次数最多的整数,并按照升序排列返回

输入:一串整型数据

每行一个整数之间用空格隔开

输出:取出整形数据中出现次数最多的整数,并按照升序排列返回

示例:

输入:
 1 1 3 4 4 4 9 9 9 10

输出:
4 9
下面参考程序是在VS2013中调试的,用了C++的STL实现:

<pre name="code" class="cpp"><span style="font-size:18px;">// 挑选出现最多的数字.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str;
while (getline(cin, str))
{
vector<int> ivec(1000, 0);
vector<int> ivec2;
//cout << str << endl;
istringstream sin(str);
int a;
while (sin >> a)
{
ivec[a]++;
//cout << a << endl;
}
int temp = 0;
for (int i = 0; i < 1000; i++)
{
if (temp < ivec[i])
{
temp = ivec[i];
ivec2.clear();
ivec2.push_back(i);
}
else if (temp == ivec[i])
{
ivec2.push_back(i);
}
}
for (int j = 0; j < ivec2.size(); j++)
{
cout << ivec2[j] << " ";
}
ivec.clear();
ivec2.clear();
str.clear();
cout << endl;
}

system("pause");
return 0;
}</span>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: