You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50 Plurality问题:修改main后代码仍未通过check50求助

CS50选举作业:print_winner函数用全局voter_count过不了check50的问题

我在写CS50的选举作业时,觉得在print_winner函数里用上main里的voter_count变量会很实用,但这个变量不像candidate_count那样在main函数之前就有声明,没法直接在print_winner里访问。

于是我在main函数之前加了全局变量声明:

//Number of votes
int voter_count;

同时把main里的这行代码:

int voter_count = get_int("Number of voters: ");

改成了:

voter_count = get_int("Number of voters: ");

自己测试所有场景代码都能正常运行,但我明知道不该修改main函数,而且就算改了,代码还是过不了check50。有人知道这是为什么吗?

我的print_winner函数代码如下:

void print_winner(void)
{
    for (int c = voter_count; (c > 0); c--)
    {
        int u = 0;

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

        if (u != 0)
        {
            return;
        }
    }
}

内容的提问来源于stack exchange,提问作者Cas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:00:56