【发布时间】:2020-02-29 10:13:18
【问题描述】:
我真的很感激有一双新的眼睛来发现我的错误! Preferences 是一个二维数组(选民与偏好),其中包含每个选民按排名顺序选择的候选人的数量(例如,preference[0][0] = i 表示 i 是第 0 个选民的首选候选人)。我没有包含排名循环,因为只有在候选人被淘汰时才会增加,并且每次考虑新选民时它都应该恢复为 0。
我已经解决这个问题很长一段时间了,但我在制表和 print_winner 的 check50 中仍然遇到一些错误。对于制表,只有在一个候选人被淘汰的情况下才会出现问题。
void tabulate(void)
{
// Set rank to equal zero first.
// This will only change if the voter's first preference is eliminated.
int rank = 0;
// Loop through voters.
for (int voter = 0; voter < voter_count; voter++)
{
for (int i = 0; i < candidate_count; i++)// Loop through candidate possibilities
{
// When candidate has been identified and is not eliminated execute this statement.
if (preferences[voter][rank] == i && candidates[i].eliminated == false)
{
candidates[i].votes += 1;
rank = 0;
// Rank is reset to zero as this may have been increased in the else if statement.
}
else if (preferences[voter][rank] == i && candidates[i].eliminated == true)
{
// If the candidate has been eliminated the rank is increased.
rank += 1;
voter -= 1;
// The voter is decreased so on returning to the 'for' loop the same voter is checked again.
}
}
}
}
【问题讨论】:
-
请比“遇到一些错误”更具体。
-
:) 当所有候选人都留在选举中时,将票数统计为表格。 :) 当一名候选人被淘汰时,制表计票。 :( 当多个候选人被淘汰时,表格会计算选票。所以我编写的代码可以工作,除非有多个候选人被淘汰出局......