蒜头君来蒜场面试的时候,曾经遇到这样一个面试题:
给定n个整数,求里面出现次数最多的数,如果有多个重复出现的数,求值最大的一个。当时可算是给蒜头君难住了。现在蒜头君来考考你。
输入格式
第一行输入一个整数n(1<=n<=100000),接下来一行输入n各int范围内的整数。
输出格式
输出出现次数最多的数和出现的次数,中间用一个空格隔开,如果有多个重复出现的数,输入值最大的那个。
样例输入1
5
1 1 2 3 4
样例输出1
1 2
样例输入2
10
9 10 27 4 9 10 3 1 2 6
样例输出2
10 2
分析:可以直接用map来解决,因为map带有排序和不重复的特性
#include<iostream>
#include<map>
using namespace std;
map<int,int>s;
int main()
{
int n,t,max=0,le=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>t;
s[t]++;
}
map<int,int>::iterator it;
for(it=s.begin();it!=s.end();it++)
{
if((it->second)>=max)
{
max=(it->second);
le=(it->first);
}
}
cout<<le<<" "<<s[le]<<endl;
return 0;
}