题目
Given an array A of N integers in wich the most frequent item appear most than n / 2 times,
calculate this value.
Input
The first line contain N (1 <= N <= 3000000) then N 32-bit integers per line.
Output
The output consists of one number: the most frequent item on A.
Example
Input:
5
2
3
2
2
4
Output:
2
AC代码
#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
multiset<int> a;
multiset<int>::iterator p;
int main()
{
int n,cnt = 0,num;
scanf("%d",&n);
while(n--)
{
int tmp;
scanf("%d",&tmp);
a.insert(tmp);
}
for(p = a.begin(); p != a.end(); p++)
{
if(a.count(*p) > cnt)
{
cnt = a.count(*p);
num = *p;
}
}
printf("%d\n",num);
return 0;
}