繁体   English   中英

CS50 pset3 径流 print_winner

[英]CS50 pset3 runoff print_winner

我找不到它为什么会抛出错误,因为当我手动输入它时它工作正常。 但是当他们使用他们的系统时会抛出一个错误。 :( print_winner 在某人拥有多数票时打印姓名 print_winner 没有打印选举获胜者 :( 当某人拥有多数票时 print_winner 返回 true print_winner 没有打印获胜者然后返回 true

int votes_need;

// printf("votes needed: %i\n", votes_need);
for (int i = 0; i < candidate_count; i++)
{
    if (voter_count % 2 != 0)
    {
        votes_need = ((voter_count + 1 )/ 2);

        if (candidates[i].votes >= votes_need)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
        else
        {
            printf("no winner yet!!!\n");
            return false;
        }
    }
    else
    {
        votes_need = ((voter_count)/ 2);

        if (candidates[i].votes > votes_need)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
        else
        {
            printf("no winner yet!!!\n");
            return false;
        }
    }
}
return false;

我认为问题在于return false;else子句; i循环内。 一旦它找到一个少于一半选票的候选人,该函数就结束并返回给调用者。 也许当你手动输入时,candidate[0] 总是赢家。

暂无
暂无

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

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