简体   繁体   English

PSET 3 Tideman CS50 lock_pairs 未正确锁定

[英]PSET 3 Tideman CS50 lock_pairs doesn't lock correctly

i am new to this site so i might not ask my question correctly which i am sorry for but i have been struggling with PSET 3 Tideman for quite a while.我是这个网站的新手,所以我可能无法正确提出我的问题,对此我很抱歉,但我已经在 PSET 3 Tideman 上苦苦挣扎了一段时间。 Currently i am on lock_pairs and i dont know what to do.目前我在 lock_pairs 上,我不知道该怎么做。 Would appreciate your help: These are my prompts for check50:感谢您的帮助:这些是我对 check50 的提示:

:) 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

void lock_pairs(void)
{
    int winner;
    int win_count[MAX];
    int temp_win = 0;
    for (int i = 0; i < pair_count; i++)
    {
        locked[ pairs[i].winner ][ pairs[i].loser ] = true;
        win_count[ pairs[i].winner ]++;
        if (win_count [ pairs[i].winner ] > temp_win )
        {
            winner = pairs[i].winner;
        }
    }
    for ( int p = 0; p < pair_count; p++)
    {
        if (win_count[ pairs[p].winner ] == win_count[winner] && pairs[p].winner != winner )
        {
            for (int i = pair_count - 1; i < -1 ; i--)
            {
                locked[ pairs[i].winner ][ pairs[i].loser ] = false;
                win_count [ pairs[i].winner ]--;
                if (pairs[i].winner == winner ) 
                {
                    win_count[winner]--;
                    for (int j = 0; j < pair_count; j++)
                    {
                        if (win_count[ pairs[j].winner ] > win_count[winner])
                        {
                            winner = pairs[j].winner;
                        }
                        if (win_count[ pairs[j].winner] == win_count[winner] && pairs[j].winner != winner)
                        {
                            break;
                        }
                        
                        
                    }
                }
                else 
                {
                    locked[ pairs[i].winner ][ pairs[i].loser ] = true;
                }
            }
        }
    }
    return;
}

You may have misunderstood the logic required behind lock_pairs , because your function is overly complicated.您可能误解了lock_pairs背后所需的逻辑,因为您的 function 过于复杂。 This is what you're asked to do-这是你被要求做的——

  • Go through each pair one by one, and lock them if necessary Go 每对一一通过,必要时锁定

  • How to know, if it is necessary ?如何知道,如果有必要 Simple, you make sure locking them does not constitute a circle很简单,您确保锁定它们不会构成一个圆圈

  • What does "constitute a circle" mean? “构成一个圆”是什么意思? This is explained very well by the course itself.课程本身很好地解释了这一点。 More info right there更多信息就在那里

    But basically,但基本上,

    How would this work in the case of the votes above?在上述投票的情况下,这将如何运作? Well, the biggest margin of victory for a pair is Alice beating Bob, since 7 voters prefer Alice over Bob (no other head-to-head matchup has a winner preferred by more than 7 voters).好吧,一对胜利的最大优势是爱丽丝击败鲍勃,因为有 7 个选民更喜欢爱丽丝而不是鲍勃(没有其他正面对决的获胜者被超过 7 个选民喜欢)。 So the Alice-Bob arrow is locked into the graph first.所以 Alice-Bob 箭头首先被锁定在图中。 The next biggest margin of victory is Charlie's 6-3 victory over Alice, so that arrow is locked in next.下一个最大的胜利是查理以 6-3 战胜爱丽丝,因此箭被锁定在下一个。

    Next up is Bob's 5-4 victory over Charlie.接下来是鲍勃以 5-4 战胜查理。 But notice: if we were to add an arrow from Bob to Charlie now, we would create a cycle, Since the graph can't allow cycles, we should skip this edge.但请注意:如果我们现在要添加一个从 Bob 到 Charlie 的箭头,我们将创建一个循环,由于该图不允许循环,我们应该跳过这条边。 and not add it to the graph at all, If there were more arrows to consider, we would look to those next, but that was the last arrow.并且根本不将其添加到图表中,如果要考虑更多箭头,我们将查看下一个箭头,但那是最后一个箭头。 so the graph is complete.所以图是完整的。

    This step-by-step process is shown below, with the final graph at right.这个分步过程如下所示,最终图表在右侧。

  • Now the real deal, how do you put this in code?现在真正的交易,你如何把它放在代码中?

    Like this-像这样-

     // Recursive function to check if entry makes a circle bool makes_circle(int cycle_start, int loser) { if (loser == cycle_start) { // If the current loser is the cycle start // The entry makes a circle return true; } for (int i = 0; i < candidate_count; i++) { if (locked[loser][i]) { if (makes_circle(cycle_start, i)) { // Forward progress through the circle return true; } } } return false; }

    Essentially, to check if locking in a winner->loser makes a circle, you walk back the entries you have locked starting from the loser and going towards all the candidates this loser has won against .本质上,要检查锁定赢家->输家是否形成了一个圆圈,您从失败者开始退回您锁定的条目,并走向该失败者赢得的所有候选人。 If you reach the winner somewhere along the way, that's a circle if I've ever seen one.如果您在途中的某个地方到达获胜者,那是一个圆圈,如果我见过的话。

    Note that circle_start though, lest it gets confusing.请注意circle_start ,以免混淆。 That's only there to remember the original circle start (which is the winner in the main loop in lock_pairs ).只是为了记住最初的循环开始(这是lock_pairs主循环中的winner )。 This is constant throughout the entire recursion.这在整个递归过程中是恒定的。 Remember, once again, our goal is to iterate through all the candidates loser has won against (and also have been locked) and make sure none of these are circle_start (aka the winner that we are about to lock in ).再一次记住,我们的目标是遍历所有loser已经战胜(并且也被锁定)的候选人,并确保这些候选人都不是circle_start (也就是我们即将锁定winner )。

So, that function right there does all the heavy lifting, your lock_pairs function is gonna be extremely simple-因此,function 就在那里完成了所有繁重的工作,您的lock_pairs function 将非常简单-

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs()
{
    for (int i = 0; i < pair_count; i++)
    {
        if (!makes_circle(pairs[i].winner, pairs[i].loser))
        {
            // Lock the pair unless it makes a circle
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CS50 - PSET 3 - Tideman - lock_pairs() 在我测试时有效,但单元测试失败 - CS50 - PSET 3 - Tideman - lock_pairs() works when I test it, but fails unit testing CS50- pset3-tideman:lock_pairs 没有正确锁定所有非循环对 - CS50- pset3-tideman: lock_pairs did not correctly lock all non-cyclical pairs CS50 的 Tideman 问题集中的 lock_pairs 函数存在缺陷 - Flaw in lock_pairs function in CS50's Tideman problem set CS50 潮人; 如果 lock_pairs 创建循环,则跳过最后一个对 - CS50 TIDEMAN; lock_pairs skips final pair if it creates cycle CS50 潮人 -:( lock_pairs 如果创建循环则跳过最后一对 - CS50 tideman - :( lock_pairs skips final pair if it creates cycle 如果 CS50 Tideman lock_pairs 创建循环并且一切正常,它会跳过最后一对 - CS50 Tideman lock_pairs skips final pair if it creates cycle & everything works as meant to CS50 Tideman,最后一个未解决的检查 - 如果 lock_pairs 创建循环,它会跳过最后一个对 - CS50 Tideman, the last unsolved check - lock_pairs skips final pair if it creates cycle CS50的pset3 Tideman...我的锁功能好像不起作用 - CS50's pset3 Tideman... my lock function doesn't seem to work Tideman 中的投票功能(CS50 的 pset 3) - Vote function in Tideman (pset 3 of CS50) 根据 CS50 的 pset3 tideman 中的 check50 排序对不起作用 - Sorting pairs is not working according to check50 in CS50's pset3 tideman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM