题解 | #超半的数#

超半的数

https://ac.nowcoder.com/acm/problem/22222

求众数,当某个数计数被其他数超过时,将其他数作为临时众数,如果题目存在超半的数,那一定存在众数,且众数为该超半的数
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;
    cin>>n;
    vector<int> l1;
    while(n--){
        cin>>k;
        l1.push_back(k);
    }
    auto it = l1.begin();
    int count=1;
    int num=*it;
    for(it++;it!=l1.end();it++)
    {
        if(count==0||num==*it){
            count++;
            num = *it;

        }
        else count--;
        
    }
    cout<<num;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务