-
时间:2016-03-30 22:03:01 星期三
-
题目编号:[2016-03-27][HDU][1029][Ignatius and the Princess IV]
-
题目大意:给定n个数字,输出其中出现次数超过n/2的数字
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1E6 + 10;
int a[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i = 1;i <= n ;++i){
scanf("%d",&a[i]);
}
sort(a,a+n);
a[0] = a[1] - 1;
int i,cnt = 0;
for(i = 1;i <= n ;++i){
if(a[i] == a[i - 1]) ++cnt;
else cnt = 1;
if(cnt >= (n + 1)/2) break;
}
printf("%d\n",a[i]);
}
return 0;
}
时间: 2024-10-05 08:23:07