选择选举获胜者的程序

时间:2018-10-26 13:03:25

标签: c++

我必须制作一个c ++程序,其中: 1)输入的第一行是两个数字;第一个(n)是候选人数(m),第二个通常是投票数; 2)第二行输入为投票 我的任务是计算选票,并说出谁是赢家;我还应该显示每个候选人的选票数。 例如:

input:
3 10
1 3 2 1 2 2 3 3 2 2 
output:
1: 2
2: 5
3: 3
2

我已经编写了一个程序,但是它并没有遍历所有机会,我也不知道如何进行开发

#include <iostream>
using namespace std;
int main() {
    int candidates;
    int votes;
    int vote[votes];
    cin >> candidates >> votes;
    int winner = 1;

    //attaching votes to a list
    for (int i = 1; i<=votes; i++) {
        cin >> vote[i];
    }; 

    //loop through candidates
    for (int k = 1; k<=candidates; k++){
        int value = 0;
        //search true the list of votes
        for (int z = 1; z<=votes; z++) {
            if(k == vote[z]) {
                value = value + 1;
            };
            cout << value << "\n";  
            if (value > winner) {
               winner = k;
            };
        };
        //give the result:
        cout << k <<":"<<value<<"\n";
    };
    cout << winner << "\n";

    return 0;
}

请帮助c:

此外,如果此人没有。 1人得3票,2号人也得3票,获胜者应是第一位候选人。我的代码无法以这种方式工作,有任何提示吗?

1 个答案:

答案 0 :(得分:0)

在C ++中,您可以使用vectors动态分配串行数据(即“可变长度数组”)。虽然修复代码对我来说很困难,但我使用向量为您的问题写了一个可能的解决方案:

radSpreadsheet.ActiveWorksheet.Cells[0, 0, rowCount - 1, columnCount - 1]?.ClearValue();
相关问题