我正在研究一个叫潮人的问题。目标是制作遵循tideman算法的投票程序。这是问题的解释:https://cs50.harvard.edu/x/2020/psets/3/tideman/ Check50(我正在参加的CS50课程提供的测试)始终返回“如果lock_pairs创建了一个循环,则会跳过中间对。lock_pairs没有正确锁定所有非周期性对“ lock_pairs函数的作用是查看获胜者和失败者的图表,并确保未创建周期(问题的链接对其进行了更好的说明)。我不完全了解跳过中间对的含义,并且该程序在测试循环时会产生循环的情况下起作用。此外,它通过了“ lock_pairs如果创建周期则跳过最后一对”检查。我通过检查所有候选者是否都具有边(俯瞰该对的当前输家)来实现lock_pairs,并且仅在并非所有候选者都具有边时才锁定该对(因此不创建循环)。我该如何解决?
void lock_pairs(void)
{
//checks if a candidate has an edge on it, originally sets all to 0
bool candidatesEdge[candidate_count];
for(int i = 0; i < candidate_count; i ++)
{
candidatesEdge[i] = false;
}
for (int i = 0; i < pair_count; i ++)
{
//variable to see if all others have edges
bool full = true;
int j = 0;
//checks through all other candidates for an edge
while (j < candidate_count && full == true)
{
//if a candidate besides the current being checked doesn't have an edge, full is false
if (j != pairs[i].loser && candidatesEdge[j] == false)
{
full = false;
}
j++;
}
//if not all candidates have edges, gives the loser of the pair an edge
if (full == false)
{
candidatesEdge[pairs[i].loser] = true;
locked[pairs[i].winner][pairs[i].loser] = true;
}
}
}