问题
hi guys im having problome with my lockpairs functinog on pset3 tideman would love some feedback ty
bool checkcycle(int from, int to)
{
if(from == to)
{
return true;
}
int i;
for (i = 0; i < candidate_count; i++)
{
if(locked[from][i])
{
checkcycle(i,to);
}
}
return false;
}
void lock_pairs(void)
{
for (int i = 0; i < candidate_count; i++)
{
if(!checkcycle(pairs[i].winner , pairs[i].loser))
{
locked[pairs[i].winner][pairs[i].loser] = true;
}
return;
}
}
:( lock_pairs locks all pairs when no cycles lock_pairs did not lock all pairs :( 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
回答1:
Your checkcycle function just need a little adjustment. I would change from
to winner
and to
to loser
. I think it would be easier to understand. Given a pair, you will call checkcycle(winner, loser)
. After checking if winner == loser
, you should iterate over all pairs checking if loser
is the winner
, and calling checkcycle(winner, loser)
, passing the same original winner, and the loser of the loser
来源:https://stackoverflow.com/questions/64155643/cs50-tideman-lock-paiors-function-issue