Description
给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。
Input
第1行一个正整数n。
第2行n个正整数用空格隔开。
Output
一行一个正整数表示那个众数。
Sample Input
5
3 2 3 1 3
Sample Output
3
HINT
100%的数据,n<=500000,数列中每个数<=maxlongint。
卡内存,解法很简单,就是将不同的数抵消,证明也很简单,因为保证有一个数大于一般,所以最坏情况就是和其他所有数抵消然后还是会剩一个。还有就是不要开namespace。
#include <cstdio>
int n,a,b,c;
int main() {
scanf("%d",&n);
while(n--) {
scanf("%d",&a);
if(a != b)
if(c) c--;
else c++, b = a;
else c++;
}
printf("%d",b);
return 0;
}