蒜头君来蒜厂面试的时候,曾经遇到这样-个面试题:
给定n个整数,求里面出现次数最多的数,如果有多个重复出现的数,求出值最大的-个。当时可算
是给蒜头君难住了。现在蒜头君来考考你。
输入格式
第一行输入一个整数n(1 S n≤100000),接下来一行输入n个int范围内的整数。
输出格式
输出出现次数最多的数和出现的次数,中间用一个空格隔开,如果有多个重复出现的数,输出值最大的那个。
#include <iostream>
#include <map>
using namespace std;
map<int, int> mp;
int main()
{
int n, x, ans1, ans2;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x;
mp[x]++;
}
ans2 = 0;
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
{
if ((it->second) >= ans2)
{
ans2 = it->second;
ans1 = it->first;
}
}
cout << ans1 << " " << ans2<<endl;
}
代码看不懂的留言哦