BZOJ2456 mode

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。

题意:找出给定数列出现次数大于数列长度一半的数(空间1M)

题目链接

 1 #include<cstdio>
 2 
 3 using namespace std;
 4 
 5 int n;
 6 
 7 int main() {
 8     scanf("%d", &n);
 9     int ans = 0, x = 0, ai;
10     while (n--) {
11         scanf("%d", &ai);
12         if (ai == ans) {
13             ++x;
14         }
15         else {
16             --x;
17             if (x <= 0) {
18                 ans = ai;
19                 x = 1;
20             }
21         }
22     }
23     printf("%d\n", ans);
24     return 0;
25 } 
View Code

猜你喜欢

转载自www.cnblogs.com/m-m-m/p/8858690.html
今日推荐