题解:P15154 [SWERC 2024] The king of SWERC

· · 题解

思路

用一个 map 存储每位候选人有多少张选票,最后用擂台法求出得票最多的人是谁即可。

代码

#include<bits/stdc++.h>
using namespace std;
map<string,int>p;
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    string s;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        p[s]++;
    }
    int maxn=0;
    for(auto it=p.begin();it!=p.end();it++)
    {
        if(it->second>maxn)
        {
            maxn=it->second;
            s=it->first;
        }
    }
    cout<<s;
    return 0;
}