【问题标题】:Logical flaw in CS50 Problem Set 3 - Tideman?CS50 问题集 3 中的逻辑缺陷 - Tideman?
【发布时间】:2020-09-28 16:41:18
【问题描述】:

我目前正在学习 CS50,这是哈佛大学提供的基于在线的编码入门模块。在为一个名为“Tideman”的问题编写代码时,我在使用 lock_pairs 函数时遇到了很多困难,因为我对编码非常陌生。可以在此处找到问题的描述:https://cs50.harvard.edu/x/2020/psets/3/tideman/

在@TomKarzes 的帮助下,我设法稍微改进了我的代码,但由于某种原因,我现在无法满足问题的要求之一,即我的代码不能正常工作,但我很肯定它几天前工作。即使我使用我的原始代码(没有 Tom 的输入),它仍然无法正常工作,所以我真的很困惑(就像......我看到了什么?)。

我有问题的功能是:

bool iscycle(int index, bool visited[])
{
    if (visited[index])
    {
        return true;
    }
    visited[index] = true;
    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[index][i] && iscycle(i, visited))
        {
            return true;
        }
    }
    return false;
}

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        locked[pairs[i].winner][pairs[i].loser] = true;
        bool visited[candidate_count];
        for (int j = 0; j < candidate_count; j++)
        {
            visited[j] = false;
        }
        if (iscycle(i, visited))
        {
            locked[pairs[i].winner][pairs[i].loser] = false;
        }
    }
    return;
}

我的整个代码(包括上面的函数)如下:

#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[])
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i], name) == 0)
        {
            ranks[rank] = i;
            return true;
        }
    }
    return false;
}

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

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

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    int swapcounter = -1;
    pair toswap;
    do
    {
        swapcounter = 0;
        for (int i = 0; i < pair_count - 1; i++)
        if (preferences[pairs[i].winner][pairs[i].loser] < preferences[pairs[i + 1].winner][pairs[i + 1].loser])
        {
            toswap = pairs[i];
            pairs[i] = pairs[i + 1];
            pairs[i + 1] = toswap;
            swapcounter++;
        }
    }
    while (swapcounter != 0);
    return;
}

bool iscycle(int index, bool visited[])
{
    if (visited[index])
    {
        return true;
    }
    visited[index] = true;
    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[index][i] && iscycle(i, visited))
        {
            return true;
        }
    }
    return false;
}

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        locked[pairs[i].winner][pairs[i].loser] = true;
        bool visited[candidate_count];
        for (int j = 0; j < candidate_count; j++)
        {
            visited[j] = false;
        }
        if (iscycle(i, visited))
        {
            locked[pairs[i].winner][pairs[i].loser] = false;
        }
    }
    return;
}

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

当我通过内置检查器 Check50 运行它以查看我的代码是否满足问题的要求时,这就是我得到的:

:) tideman.c exists
:) tideman compiles
:) vote returns true when given name of candidate
:) vote returns false when given name of invalid candidate
:) vote correctly sets rank for first preference
:) vote correctly sets rank for all preferences
:) record_preferences correctly sets preferences for first voter
:) record_preferences correctly sets preferences for all voters
:) add_pairs generates correct pair count when no ties
:) add_pairs generates correct pair count when ties exist
:) add_pairs fills pairs array with winning pairs
:) add_pairs does not fill pairs array with losing pairs
:) sort_pairs sorts pairs of candidates by margin of victory
:) 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
:) print_winner prints winner of election when one candidate wins over all others
:) print_winner prints winner of election when some pairs are tied

我无法理解我的逻辑缺陷在哪里。

附:如果有人能花时间向我解释,在效率和“代码设计”方面,当递归优于迭代时,反之亦然,那也将不胜感激!

【问题讨论】:

标签: c loops recursion iteration cs50


【解决方案1】:

我一直在努力使用这个功能。 基本上,在这个递归函数中,如果其中一个胜利者等于失败者,则必须返回 TRUE,而不是所有圈子都被锁定。 但是您正在检查访问过的数组,我也是通过“已访问”路径进行的,我花了一些时间寻找另一种方式:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 2017-07-16
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多