CS50 潮汐人:sort_pairs、lock_pairs和print_winner错误

CS50 tideman: sort_pairs, lock_pairs and print_winner error

提问人:tiant 提问时间:11/12/2023 最后编辑:Some programmer dudetiant 更新时间:11/12/2023 访问量:40

问:

我在 CS50 中做了 tideman,但我不知道为什么我错了。 请帮帮我~非常感谢! 这是我的代码和结果:

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

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
} pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: tideman [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] = argv[i + 1];
    }

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

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

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(name, candidates[i]) == 0)
        {
            ranks[rank] = i;
            return true;
        }
    }
    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            int p = ranks[i];
            int q = ranks[j];
            preferences[p][q]++;
        }
    }
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO
    pair_count = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = i; j < candidate_count; j++)
        {
            if (preferences[i][j] > preferences[j][i])
            {
                pair_count++;
                pairs[pair_count - 1].winner = i;
                pairs[pair_count - 1].loser = j;
            }
            else if (preferences[i][j] < preferences[j][i])
            {
                pair_count++;
                pairs[pair_count - 1].winner = j;
                pairs[pair_count - 1].loser = i;
            }
        }
    }
    return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO
    for (int j = 0; j < pair_count; j++)
    {
        for (int i = 0; i < pair_count - j - 1; i++)
        {
            int p = pairs[i].winner;
            int q = pairs[i].loser;
            int m = pairs[i + 1].winner;
            int n = pairs[i + 1].loser;

            if (preferences[p][q] > preferences[m][n])
            {
                int temp = preferences[p][q];
                preferences[p][q] = preferences[m][n];
                preferences[m][n] = temp;
            }
        }
    }
    return;
}

// Lock pairs into the candidate graph in order, without creating cycles

bool is_cycle(int candidate_a, int candidate_b)
{
    if (candidate_a == candidate_b)
    {
        return true;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[candidate_b][i])
        {
            if (is_cycle(i, candidate_a))
            {
                return true;
            }
        }
    }
    return false;
}

void lock_pairs(void)
{
    // TODO
    for (int i = 0; i < pair_count; i++)
    {
        if (!is_cycle(pairs[i].winner, pairs[i].loser))
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }
    return;
}

// Print the winner of the election
void print_winner(void)
{
    // TODO
    int fail_count = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (!locked[j][i])
            {
                fail_count++;
            }
        }
        if (fail_count == candidate_count)
        {
            printf("%s\n", candidates[i]);
        }
    }
    return;
}

检查结果50

我的错:

:( sort_pairs sorts pairs of candidates by margin of victory
    sort_pairs did not correctly sort pairs
:) lock_pairs locks all pairs when no cycles
:( lock_pairs skips final pair if it creates cycle
    lock_pairs did not correctly lock all non-cyclical pairs
:( lock_pairs skips middle pair if it creates a cycle
    lock_pairs did not correctly lock all non-cyclical pairs
:) print_winner prints winner of election when one candidate wins over all others
:( print_winner prints winner of election when some pairs are tied
    print_winner did not print winner of election

我问过 GPT,但它没有用。我搜索了一些答案,但我仍然不知道为什么我错了。

中C CS50

评论

0赞 Some programmer dude 11/12/2023
CS50 对全局变量有什么看法?如果它没有说它们是一个坏习惯,应该避免,那么它比我以前认为的还要糟糕。
0赞 Some programmer dude 11/12/2023
至于问题和代码,退后几步,用一个空函数重新开始。在本地生成,并启用额外的警告并将其视为错误。只有当它干净地构建时,你才能继续测试程序。然后添加一小段代码。一个易于测试和调试的产品。像以前一样生成和测试。除非它干净地构建并通过所有测试,否则不要继续。这样一来,查找、调试和修复问题就容易多了。main
0赞 Some programmer dude 11/12/2023
当您遇到问题时,请学习如何调试您的程序。这是程序员需要的最重要的技能之一。例如,使用调试器逐行执行代码,同时监视变量及其值。使用笔和纸来跟踪事情。
0赞 Some programmer dude 11/12/2023
顺便说一句,检查允许您用作数组的索引。相反,您需要。if (candidate_count > MAX)MAXif (candidate_count >= MAX)

答: 暂无答案