我在pset3中使用CS50的tideman遇到了一些问题。 除了最后一个,check50中的所有内容都是正确的:
:( print_winner prints winner of election when some pairs are tied.
print_winner did not print winner of election
这是我的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max number of candidates
#define MAX 9
// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];
// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];
// Each pair has a winner, loser
typedef struct
{
int winner;
int loser;
}
pair;
// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];
int pair_count;
int candidate_count;
// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: tideman [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i] = argv[i + 1];
//printf("Candidate %i: %s\n", i + 1, argv[i + 1]);
}
// Clear graph of locked in pairs
for (int i = 0; i < candidate_count; i++)
{
for (int j = 0; j < candidate_count; j++)
{
locked[i][j] = false;
}
}
pair_count = 0;
int voter_count = get_int("Number of voters: ");
// Query for votes
for (int i = 0; i < voter_count; i++)
{
// ranks[i] is voter's ith preference
int ranks[candidate_count];
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
if (!vote(j, name, ranks))
{
printf("Invalid vote.\n");
return 3;
}
}
record_preferences(ranks);
printf("\n");
}
add_pairs();
sort_pairs();
lock_pairs();
print_winner();
return 0;
}
// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(name, candidates[i]) == 0)
{
ranks[rank] = i;
return true;
}
}
return false;
}
// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
// TODO
int counter = 0;
for (int i = 0; i < candidate_count; i++)
{
int j = 1;
for (j += counter; j < candidate_count; j++)
{
preferences[ranks[i]][ranks[j]] += 1;
}
counter++;
}
return;
}
// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
// TODO
int k = 0;
int l = 1;
for (int i = 0; i < candidate_count - 1; i++)
{
for (int j = l; j < candidate_count; j++)
{
if (preferences[i][l] > preferences[l][i])
{
pairs[k].winner = i;
pairs[k].loser = l;
pair_count++;
k++;
}
else if (preferences[i][l] < preferences[l][i])
{
pairs[k].loser = i;
pairs[k].winner = l;
pair_count++;
k++;
}
if (l < candidate_count - 1)
{
l++;
}
}
}
return;
}
// Sort pairs in decreasing order by strength of victory
//int strength [MAX];
pair temp[1];
void sort_pairs(void)
{
// TODO
for (int j = 0; j < candidate_count - 1; j++)
{
//if (strength[j] < strength[j + 1])
if (preferences[pairs[j].winner][pairs[j].loser] < preferences[pairs[j + 1].winner][pairs[j + 1].loser])
{
temp[0] = pairs[j + 1];
pairs[j + 1] = pairs[j];
pairs[j] = temp[0];
}
}
return;
}
// Lock pairs into the candidate graph in order, without creating cycles
pair first[1];
void lock_pairs(void)
{
// TODO
first[0] = pairs[0];
locked[pairs[0].winner][pairs[0].loser] = true;
for (int i = 1; i < candidate_count; i++)
{
if (pairs[i].loser == pairs[i - 1].winner)
{
first[0] = pairs[i];
}
locked[pairs[i].winner][pairs[i].loser] = true;
}
for (int j = 1; j < candidate_count; j++)
{
if (pairs[j].loser == first[0].winner)
{
locked[pairs[j].winner][pairs[j].loser] = false;
}
}
return;
}
// Print the winner of the election
void print_winner(void)
{
// TODO
printf("%s\n", candidates[first[0].winner]);
return;
}
据我所知,如果成对绑在一起,它们具有相同的强度吗?实力是仅意味着对获胜者的总体偏好,还是获胜者与失败者之间的差异?例如,如果查理(Charlie)赢得爱丽丝(Alice)7-2,则力量是5还是7?
我不太了解是什么原因导致此错误,请帮忙吗?
答案 0 :(得分:-1)
只是碰到了这个线程,这挽救了我的理智:
https://www.reddit.com/r/cs50/comments/enm5t8/pset3_tideman_winner_when_some_pairs_are_tied/
事实证明,您毕竟需要考虑多个来源!