简体   繁体   English

CS50 复数 - 如果获奖者太多,则无法打印

[英]CS50 plurality - unable to print if there are too many winners

In plurality prblm, I managed to update votes for each candidate, my code can print the one winner, but still stuck if they are many winners .在多个prblm中,我设法更新了每个候选人的投票,我的代码可以打印一个获胜者,但如果他们有很多获胜者,仍然会卡住。 help by hints or clues, not the whole solution.通过提示或线索提供帮助,而不是整个解决方案。 Thanks in advance.提前致谢。

void print_winner(void)
{
    int v = 0; //maximum number of votes
    string w; //winner of the election
    for (int i = 0; i < candidate_count; i++)
    {
        if (v <= candidates[i].votes)
        {
            v = candidates[i].votes;
        }
    }
    

    for (int j = 0; j < candidate_count; j++)
    {
        if (candidates[j].votes == v)
        {
            w = candidates[j].name;
        }
    }
    printf("%s\n", w);
    return;
}

Change your second for loop to this:将您的第二个for循环更改为:

for (int j = 0; j < candidate_count; j++)
{
    if (candidates[j].votes == v)
    {
        w = candidates[j].name;
        printf("%s\n", w);
    }
}

You need to separate it into two for loops, if you have them together the first loop will run only through the first candidate with more than 1 vote and print it.您需要将其分成两个 for 循环,如果将它们放在一起,则第一个循环将仅通过第一个获得超过 1 票的候选人运行并打印。 Since it has not checked the others.因为它没有检查其他人。 That loops must finish and go through the whole set to actually find the max number of votes.该循环必须完成并遍历整个集合才能实际找到最大票数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM