CS50 Tideman :( 如果 lock_pairs 创建了循环,则 lock_pairs 会跳过最终对 lock_pairs 未正确锁定所有非循环对

问题描述 投票:0回答:1

有人可以帮助我理解我的代码有什么问题吗 我正在研究这个问题集https://cs50.harvard.edu/x/2023/psets/3/tideman/

bool CycleCheckRecursion(int L, int W)
// Checks if there is a cycle. Returns 1 if cycle is found.
{
    for (int q = 0;  q <= pair_count - 1; q++)
    {
        if (locked[q][W] == true)
        {
            if (locked[L][q] == true)
                {
                    return 1;
                }
                else
                {
                    CycleCheckRecursion(L, q);
                }
        }
    }
    return 0;
}
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{        // TODO
    for (int y = 0; y <= pair_count - 1; y++)
    {
        if (!CycleCheckRecursion(pairs[y].loser, pairs[y].winner))
        {
            locked[pairs[y].winner][pairs[y].loser] = true;
        }
    }
}

这是运行 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

中间对按预期被跳过,但由于某种原因最后一对没有被跳过。有人可以帮助我了解我的代码的哪一部分是错误或丢失的吗?

c recursion cs50
1个回答
0
投票

我在@Someprogrammerdude 和chatgpt 的帮助下解决了这个问题。

bool CycleCheckRecursion(int L, int W)
// Checks if there is a cycle. Returns 1 if cycle is found.
{
    for (int q = 0;  q <= pair_count - 1; q++)
    {
        if (locked[q][W] == true)
        {
            if (locked[L][q] == true)
                {
                    return 1;
                }
                else
                {
                    if(CycleCheckRecursion(L, q))
                    return 1;
                }
        }
    }
    return 0;
}
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{        // TODO
    for (int y = 0; y <= pair_count - 1; y++)
    {
        if (!CycleCheckRecursion(pairs[y].loser, pairs[y].winner))
        {
            locked[pairs[y].winner][pairs[y].loser] = true;
        }
    }
}

我需要做的就是打电话

            {
                CycleCheckRecursion(L, q);
            }

再次查看它返回 true 还是 false。所以我把它放在 if 语句中并检查它是否返回 true。然后让它在返回true的时候返回true。 这是下面的更改

            {
                if(CycleCheckRecursion(L, q))
                return 1;
            }

现在我从终端收到此消息

:) lock_pairs skips final pair if it creates cycle
© www.soinside.com 2019 - 2024. All rights reserved.