[bzoj2456]mode

因为这组数中的众数超过一半,所以可以开一个变量记一下当前的数字以及当前数字数量(看代码),由于数组中的众数的特性,其他的数字都会被众数抵消掉,所以只剩下了众数。(口胡)

 1 /*
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <map>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <cmath>
11 #include <queue>
12 */
13 #include <cstdio>
14 using namespace std;
15 
16 int n,tmp,t;
17 int main(){
18     scanf("%d",&n);
19     for(int i=1;i<=n;i++){
20         int x;scanf("%d",&x);
21         if(tmp==0)t=x,tmp=1;
22         else if(t==x)tmp++;
23         else tmp--;
24     }
25     printf("%d\n",t);
26 }
View Code

 

posted @ 2017-01-01 15:22  KingSann  阅读(83)  评论(0)    收藏  举报