C++且不使用vector头文件完成:M国最近马上要开始国家领导大选啦关于候选人一共有n位需要通过投票决定谁当选最后的国家总统但是选票过多人工筛选太过于繁琐故请你设计一个程序输入参选人数n以及其名字并按输入顺序进行编号。随后输入m代表有m张有效选票接下来输入每个选票对应的参选人编号。最后所得票数最多的最终当选总统。注意:本题中不会出现票数相同的情况。输入描述n+3行第1行包含一个整数n代表n位参选
#include <iostream>
#include <string>
#include <map>
int main() {
int n;
std::cin >> n;
std::map<int, std::string> candidates;
for (int i = 0; i < n; i++) {
std::string name;
std::cin >> name;
candidates[i+1] = name;
}
int m;
std::cin >> m;
std::map<int, int> votes;
for (int i = 0; i < m; i++) {
int candidate;
std::cin >> candidate;
votes[candidate]++;
}
int maxVotes = 0;
int winner = 0;
for (const auto& vote : votes) {
if (vote.second > maxVotes) {
maxVotes = vote.second;
winner = vote.first;
}
}
std::cout << winner << " " << candidates[winner] << std::endl;
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/ifO2 著作权归作者所有。请勿转载和采集!