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

发布于 2025-01-13 15:51:12 字数 582 浏览 4 评论 0原文

在多数 prblm 中,我设法更新每个候选人的选票,我的代码可以打印一个获胜者,但如果他们有很多获胜者,仍然会卡住。 通过提示或线索提供帮助,而不是整个解决方案。 提前致谢。

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;
}

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 .
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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白日梦 2025-01-20 15:51:12

将第二个 for 循环更改为:

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

Change your second for loop to this:

for (int j = 0; j < candidate_count; j++)
{
    if (candidates[j].votes == v)
    {
        w = candidates[j].name;
        printf("%s\n", w);
    }
}
薄凉少年不暖心 2025-01-20 15:51:12

您需要将其分成两个 for 循环,如果将它们放在一起,第一个循环将仅运行第一个拥有超过 1 票的候选人并打印它。因为它没有检查其他人。该循环必须完成并遍历整个集合才能真正找到最大票数。

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. 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.

撧情箌佬 2025-01-20 15:51:12

我也和你遇到同样的问题大约两三天了。但我想我找到了如何打印有多个获奖者的情况。

首先,我有一个变量来存储最高票数并将其初始化为 0。然后我尝试将候选人[i].votes 与当前最高票数进行比较。

然后,我将最高票数的新值与候选人的投票值一起分配,直到循环找到比前一个票数更高的人。

最后,我再次循环遍历所有候选人并检查每个候选人的投票是否等于最高票数。如果是,那么他们的名字应该被打印出来。这解决了我打印 2 个或更多获胜者的问题。

I am also on the same problem with you for around 2 or 3 days now. But I think I found out how to print if there are multiple winners.

First, I have a variable to store the highest votes and initialized it to 0. Then I tried comparing the candidates[i].votes against the current highest vote.

Then, I assigned the new value of the highest vote with the candidates vote until the loop finds someone who is higher than the previous one.

Lastly, I looped again through all the candidates and checks each candidates votes if they are equal to the highest vote. If yes, then their names should be printed. This solved my problem in printing 2 or more winners.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文