"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
3 5 1【题解】给出n个数,要你找出这n个数中出现次数不少于(n+1)/2的数,有且仅有一个。
题目给的数据范围很大,所以用普通的数组记录做法会超时,所以可以考虑用map容器。
【AC代码】
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
int m,n;
int main()
{
int a,s;
while(~scanf("%d",&m)){
n=m;
map<int,int>p;
while(m--)
{
scanf("%d",&a);
p[a]++;
if(p[a]>=(n+1)/2){
s=a;
}
}printf("%d\n",s);
}
return 0;
}