问题描述
您好,我在pset3 tideman上的我的密码对functinog遇到问题了
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在没有循环时锁定所有对lock_pairs没有锁定所有对:(如果创建周期,lock_pairs会跳过最后一对lock_pairs没有正确锁定所有非周期性对:(如果创建一个循环,lock_pairs会跳过中间对lock_pairs没有正确锁定所有非周期性对
:( lock_pairs locks all pairs when no cycleslock_pairs did not lock all pairs:( lock_pairs skips final pair if it creates cyclelock_pairs did not correctly lock all non-cyclical pairs:( lock_pairs skips middle pair if it creates a cyclelock_pairs did not correctly lock all non-cyclical pairs
推荐答案
您的checkcycle函数仅需进行一些调整.我会将从
更改为 winner
,将更改为
loser
.我认为这会更容易理解.给定一对,您将呼叫 checkcycle(获胜者,失败者)
.检查 winner ==失败者
后,您应该遍历所有对,检查 loser
是否是 winner
,然后调用 checkcycle(winner,失败者)
,传递相同的原始获胜者,以及失败者的失败者
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
这篇关于cs50 tideman lock_paiors函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!