{"title": "C++ 候选人投票系统 (无 vector 或 map) - 找出获胜者", "description": "本代码使用 C++ 实现一个简单的投票系统,找出获得最多票数的总统候选人。它使用数组来存储候选人信息和票数,无需使用 vector 或 map。", "keywords": "C++, 投票系统, 候选人, 投票, 获胜者, 数组, 算法, 程序设计", "content": "#include \n#include \n\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n\n string candidates[n];\n for (int i = 0; i < n; i++) {\n cin >> candidates[i];\n }\n\n int m;\n cin >> m;\n\n int votes[n] = {0};\n for (int i = 0; i < m; i++) {\n int candidateIndex;\n cin >> candidateIndex;\n votes[candidateIndex - 1]++;\n }\n\n int maxVotes = 0;\n int presidentIndex = 0;\n for (int i = 0; i < n; i++) {\n if (votes[i] > maxVotes) {\n maxVotes = votes[i];\n presidentIndex = i;\n }\n }\n\n cout << presidentIndex + 1 << " " << candidates[presidentIndex] << endl;\n\n return 0;\n}\n"}

C++ 候选人投票系统 (无 vector 或 map) - 找出获胜者

原文地址: https://www.cveoy.top/t/topic/pYez 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录