【BZOJ】2456 mode(乱搞)

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

----------------------------------------------------------------------

分析:这真是一道妙题啊。。。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int a,tmp,n,num,f=0;
 7     scanf("%d",&n);
 8     for(int i=0;i<n;i++)
 9     {
10         scanf("%d",&a);
11         if(f<=0)
12         {
13             f=1;
14             num=a;
15             continue;
16         }
17         if(a==num) f++;
18         else f--;
19     }
20     printf("%d",num);
21     return 0;
22 }
原文地址:https://www.cnblogs.com/noblex/p/7658003.html