我刚开始使用stackoverflow,并且对编程也很陌生。我正在为CS50课程解决tideman问题。https://cs50.harvard.edu/x/2020/psets/3/tideman/ 当我运行check50时,所有的检查项都通过了,除了一个:
:( lock_pairs跳过了最后一对如果创建了循环 lock_pairs没有正确锁定所有非循环对
这两个测试都通过了: :) lock_pairs在没有循环的情况下锁定了所有对 :) lock_pairs跳过了中间的一对,如果它会创建一个循环
我找不到问题所在。我错过了什么?
这是我的代码:
// 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];
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
// for every pair we need to check for a circle
for (int i = 0; i < pair_count; i++)
{
if (!circle_check(pairs[i].winner, pairs[i].loser))
{
//there's no circle: lock in pair
locked[pairs[i].winner][pairs[i].loser] = true;
}
}
}
// check pair for circles between winner and loser. Loser is first link
bool circle_check(int winner, int link)
{
// check if the loser already has connections
for (int n = 0; n < candidate_count; n++)
{
if (locked[link][n] == true)
{
// there's a link. if this ends in the winner, there's a circle
if (n == winner)
{
return true;
}
else
{
// there may still be a circle, check next connection
link = n;
circle_check(winner, link);
}
}
}
return false;
}