C++ 编程题:班委选举计票
{
"title": "C++ 编程题:班委选举计票",
"description": "这是一道 C++ 编程题,要求编写一个程序实现班委选举的计票功能,程序需要读取每个同学的编号和投票结果,统计每个同学的得票数,并输出得票最多的同学编号。",
"keywords": "C++编程, 计票, 班委选举, 算法",
"content": "思路:\n1. 首先,读取输入的n和m。\n2. 然后,读取m个选票上所写的编号,并统计每个编号出现的次数。\n3. 接下来,遍历统计结果,找到得票最多的编号。\n4. 最后,输出得票最多的编号。\n\n代码实现如下:\n\ncpp\n#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n vector<int> votes(n + 1, 0); // votes[i]表示编号为i的同学的得票数\n\n for (int i = 0; i < m; i++) {\n int num;\n cin >> num;\n votes[num]++;\n }\n\n int maxVotes = 0; // 最多的得票数\n int maxNum = 0; // 得票最多的编号\n\n for (int i = 1; i <= n; i++) {\n if (votes[i] > maxVotes) {\n maxVotes = votes[i];\n maxNum = i;\n }\n }\n\n cout << maxNum << endl;\n\n return 0;\n}\n"\n}
原文地址: https://www.cveoy.top/t/topic/qr2S 著作权归作者所有。请勿转载和采集!