Time Limit: 1 Sec Memory Limit: 1 MB
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。
zju2132 The Most Frequent Number
#include <cstdio>
using namespace std;
int n;
int tot=0;
int h;
int xr;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&h);
if(!tot){
xr=h;
tot=1;
}
if(h==xr) tot++;
else if(h!=xr) tot--;
}
printf("%d",xr);
return 0;
}
第一眼看上去以为这是一道傻子题,但wa了2次后发现,这题并不能开数组~~~~哈哈哈哈,把每个输入的数互相抵消一下,,,,,这真是神**算法