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

CS50 Problem Set3代码本地正常但check50检测print_winner报错

CS50 Plurality项目print_winner函数的check50报错问题

我正在学习CS50课程,目前进行到Problem Set3的plurality项目。本地测试示例能得到正确结果,但使用check50检测时,始终存在一个报错:print_winner识别Alice为选举胜者的测试不通过,提示print_winner function did not print winner of election。

我的代码

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

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

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    bool check = false;
    for (int j = 0; j < candidate_count; j++)
    {
        if (strcmp(candidates[j].name, name) == 0)
        {
            candidates[j].votes = candidates[j].votes + 1;
            check = true;
            break;
        }
    }
    return check;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int count = 0;
    int equal_count = 0;
    string s;
    for (int k = 0; k < candidate_count - 1; k++)
    {
        if (candidates[k].votes > candidates[k + 1].votes)
        {
            count = candidates[k].votes;
        }
        else if (candidates[k + 1].votes > candidates[k].votes)
        {
            count = candidates[k + 1].votes;
        }
        else
        {
            equal_count = candidates[k + 1].votes;
        }
    }
    if (equal_count > count)
    {
        count = equal_count;
    }
    for (int j = 0; j < candidate_count; j++)
    {
        if (candidates[j].votes == count)
        {
            s = candidates[j].name;
            printf("%s\n", s);
        }
    }

    return;
}

本地运行示例结果

$ ./plurality Alice Bob Charlie
Number of voters: 4
Vote: Alice
Vote: Bob
Vote: Charlie
Vote: Alice
Alice

check50检测结果

:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:( print_winner identifies Alice as winner of election
    print_winner function did not print winner of election
:) print_winner identifies Bob as winner of election
:) print_winner identifies Charlie as winner of election
:) print_winner prints multiple winners in case of tie
:) print_winner prints all names when all candidates are tied

问题分析与修复

你的print_winner函数计算最高票数的逻辑存在严重漏洞:

  1. 单候选人场景失效:当只有Alice一个候选人时,candidate_count - 1为0,循环k < 0不会执行,此时count保持初始值0,而Alice的票数肯定大于0,导致不会打印她的名字,这正是check50测试失败的原因。
  2. 无法正确获取全局最高票数:通过相邻元素比较的方式,会导致最终count变成最后一对比较中的较大值,而非全局最大值。比如候选人票数为[5,3,2]时,循环结束后count会被更新为3,而非正确的5。

修复后的print_winner函数

void print_winner(void)
{
    // 遍历所有候选人,找到最高票数
    int max_votes = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > max_votes)
        {
            max_votes = candidates[i].votes;
        }
    }

    // 打印所有票数等于最高票数的候选人
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == max_votes)
        {
            printf("%s\n", candidates[i].name);
        }
    }

    return;
}

这个逻辑直接可靠:

  • 第一次遍历找出全局最高票数max_votes;
  • 第二次遍历打印所有票数等于max_votes的候选人,覆盖了单候选人、多候选人、平局等所有场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 20:37:00