这是我写的代码,结果完全不正确,求解答,我循环输出b数组的时候数据也不对
下面的代码结果对的:
#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