Skip to content

M24684: 直播计票

http://cs101.openjudge.cn/practice/24684/

思路:用字典的方式记录选票,在查询最大票数的同时记录答案数组

cpp
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
    map<int, int> count;  // 选项编号 -> 票数
    int x;
    
    while (cin >> x) 
    {
        count[x]++;
    }
    
    int max_votes = 0;
    vector<int> ans_vec;
    for (auto& p : count) 
    {
        if (p.second > max_votes) 
        {
            max_votes = p.second;
            ans_vec.clear();
            ans_vec.push_back(p.first);
        }
        else if (p.second == max_votes)
        {
            ans_vec.push_back(p.first);
        }
    }
    
    bool isFirst = true;
    for (auto& ans : ans_vec)
    {
        if (!isFirst)
        {
            cout << " ";
        }
        cout << ans;
        isFirst = false;
    }

    return 0;
}