这是我写的代码,结果完全不正确,求解答,我循环输出b数组的时候数据也不对
1条回答 默认 最新
- 小公鸡卡哇伊呀~ 2021-10-25 20:23关注
下面的代码结果对的:
#include <iostream> using namespace std; int main() { int candidates[6] = {0}; int n = 0; cin >> n; int x; for (int i=1; i <=n; i++) { cin >> x; candidates[x]++; // 投的谁的票, 谁的票数加1 } int max=0; int maxIndex=0; for (int i=1; i <= 5; i++) { if (max <= candidates[i]) { max = candidates[i]; maxIndex = i; } } cout << "winner: " << maxIndex << " with " << max << " votes" << endl; return 0; }
运行结果:
10
1 1 1 1 1 2 2 3 4 5
winner: 1 with 5 votes本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报