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