[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

 

转载于:https://www.cnblogs.com/KingSann/articles/6241074.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值