Description
给出n个数,求其中出现次数最多的数字,如果有多种方案,则输出最先达到最大数量的数字
Input
第一个一整数n表示数字数量,之后n个整数a[i] (1<=n<=1000,1<=a[i]<=1e6)
Output
输出答案
Sample Input
5
1 3 2 2 1
Sample Output
2
Solution
拿map存一下每个数的数量和其最后出现的位置,找数量最大的和最后出现位置最小的
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111
map<int,int>m,mm;
map<int,int>::iterator it;
int n;
int main()
{
while(~scanf("%d",&n))
{
m.clear(),mm.clear();
int a;
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
m[a]++,mm[a]=i;
}
int ans=0,pos;
for(it=m.begin();it!=m.end();it++)
if(it->second>m[ans]||it->second==m[ans]&&mm[it->first]<pos)
ans=it->first,pos=mm[it->first];
printf("%d\n",ans);
}
return 0;
}