cs50 tideman lock_paiors function issue

馋奶兔 提交于 2021-01-20 09:20:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!