Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
70874 刘焕群 19选班长II C++ Wrong Answer 1 MS 272 KB 1380 2024-04-20 12:08:55

Tests(0/10):


Code:

#include <iostream> using namespace std; int main() { const int NUM_CANDIDATES = 3; // 候选人的数量 int votes[NUM_CANDIDATES] = {0}; // 存储每个候选人的得票数 int totalVotes = 0; // 总票数 int currentVote; // 当前读取到的选票 // 读取选票,直到读到-1 while (cin >> currentVote && currentVote != -1) { if (currentVote >= 1 && currentVote <= NUM_CANDIDATES) { votes[currentVote - 1]++; // 更新对应候选人的票数,数组索引从0开始 totalVotes++; // 总票数加一 } } // 输出每个候选人的得票数 for (int i = 0; i < NUM_CANDIDATES; ++i) { cout << "Candidate " << char('A' + i) << ": " << votes[i] << endl; } // 检查是否有人得票数过半 bool elected = false; for (int i = 0; i < NUM_CANDIDATES; ++i) { if (votes[i] > totalVotes / 2) { // 注意:这里用整数除法,所以如果有偶数票则会自动向下取整 cout << "Candidate " << char('A' + i) << "-yes" << endl; elected = true; break; } } // 如果没有人得票数过半,则输出all-NO if (!elected) { cout << "all-NO" << endl; } return 0; }


Run Info:

------Input------
4 3 4 2 1 3 1 4 1 4 1 2 1 3 3 1 3 4 3 1 4 4 4 4 4 4 4 4 1 1 4 4 1 2 4 3 4 2 1 2 1 3 3 3 -1
------Answer-----
A=12 B=5 C=10 Tot=44 all-NO
------Your output-----
Candidate A: 12 Candidate B: 5 Candidate C: 10 all-NO