CS50 pset3 Check50 正在运行,但是当我测试我的代码时出现问题
CS50 pset3 Check50 is working but when I test my code there is something wrong
编写这段代码我一直用 check50
检查我的程序,这些是我的结果:
:) runoff.c exists
:) runoff compiles
:) vote returns true when given name of candidate
:) vote returns false when given name of invalid candidate
:) vote correctly sets first preference for first voter
:) vote correctly sets third preference for second voter
:) vote correctly sets all preferences for voter
:) tabulate counts votes when all candidates remain in election
:) tabulate counts votes when one candidate is eliminated
:) tabulate counts votes when multiple candidates are eliminated
:) tabulate handles multiple rounds of preferences
:) print_winner prints name when someone has a majority
:) print_winner returns true when someone has a majority
:) print_winner returns false when nobody has a majority
:) print_winner returns false when leader has exactly 50% of vote
:) find_min returns minimum number of votes for candidate
:) find_min returns minimum when all candidates are tied
:) find_min ignores eliminated candidates
:) is_tie returns true when election is tied
:) is_tie returns false when election is not tied
:) is_tie returns false when only some of the candidates are tied
:) is_tie detects tie after some candidates have been eliminated
:) eliminate eliminates candidate in last place
:) eliminate eliminates multiple candidates in tie for last
:) eliminate eliminates candidates after some already eliminated
它似乎可以工作,但是当我用 cs50 示例测试它时,输出是不同的。
我该怎么办?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];
// Candidates have name, vote count, eliminated status
typedef struct
{
string name;
int votes;
bool eliminated;
}
candidate;
// Array of candidates
candidate candidates[MAX_CANDIDATES];
// Numbers of voters and candidates
int voter_count;
int candidate_count;
// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: runoff [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX_CANDIDATES)
{
printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
candidates[i].eliminated = false;
}
voter_count = get_int("Number of voters: ");
if (voter_count > MAX_VOTERS)
{
printf("Maximum number of voters is %i\n", MAX_VOTERS);
return 3;
}
// Keep querying for votes
for (int i = 0; i < voter_count; i++)
{
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
// Record vote, unless it's invalid
if (!vote(i, j, name))
{
printf("Invalid vote.\n");
return 4;
}
}
printf("\n");
}
// Keep holding runoffs until winner exists
while (true)
{
// Calculate votes given remaining candidates
tabulate();
// Check if election has been won
bool won = print_winner();
if (won)
{
break;
}
// Eliminate last-place candidates
int min = find_min();
bool tie = is_tie(min);
// If tie, everyone wins
if (tie)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
printf("%s\n", candidates[i].name);
}
}
break;
}
// Eliminate anyone with minimum number of votes
eliminate(min);
// Reset vote counts back to zero
for (int i = 0; i < candidate_count; i++)
{
candidates[i].votes = 0;
}
}
return 0;
}
// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
preferences[voter][rank] = i;
return true;
}
}
return false;
}
// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
for (int i = 0; i < voter_count; i++)
{
for (int j = 0; j < candidate_count; j++)
{
if (! candidates[preferences[i][j]].eliminated)
{
candidates[preferences[i][j]].votes++;
break;
}
}
}
}
// Print the winner of the election, if there is one
bool print_winner(void)
{
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > voter_count / 2)
{
printf("%s\n", candidates[i].name);
return true;
}
}
return false;
}
// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
int min = 0;
int x;
int y;
for (int i = 0; i < candidate_count - 1; i++)
{
for (int j = i + 1; j < candidate_count; j++)
{
if (candidates[j].votes < candidates[i].votes)
{
// swapping elements
x = candidates[i].eliminated;
candidates[i].eliminated = candidates[j].eliminated;
candidates[j].eliminated = x;
y = candidates[i].votes;
candidates[i].votes = candidates[j].votes;
candidates[j].votes = y;
}
}
}
for (int k = 0; k < candidate_count; k++)
{
if (!candidates[k].eliminated)
{
min = candidates[k].votes;
break;
}
}
return min;
}
// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
int ties = 0;
int elim = 0;
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
if (candidates[i].votes == min)
{
ties++;
}
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].eliminated)
{
elim++;
}
}
if (ties == candidate_count - elim)
{
return true;
}
return false;
}
// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
if (candidates[i].votes == min)
{
candidates[i].eliminated = true;
}
}
}
return;
}
例如使用这些输入:
./runoff Alice Bob Charlie
Number of voters: 5
Rank 1: Alice
Rank 2: Bob
Rank 3: Charlie
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Bob
Rank 2: Alice
Rank 3: Charlie
Rank 1: Charlie
Rank 2: Alice
Rank 3: Bob
程序应该 returns Alice
,但它 returns Bob
,我的代码有什么问题?
find_min 中的位交换候选不交换名称。
有问题:
用你的例子输入,在find_min()之前第一轮候选人[]处于以下状态:Alice,2;Bob,2;Charlie,1
(此时也到处都是假的,我在这里省略了)
find_min() 将其更改为 Alice,1;Bob2;Charlie,2
– 看看这里出了什么问题?查理犯下选举舞弊并进入下一次决选。
程序继续淘汰 Alice 并再次计票,Bob 获得 3 票,Charlie 获得 2 票(从那里开始它的工作与预期的差不多)。
排序时交换时出现错误。
编辑:
当您将候选人[i] 与候选人[j] 交换时(顺便说一句,您可以在 1 步而不是 3 步中使用 tmp 候选人),这会扰乱选民的偏好。
例如:第一个投票者正在投票给 A、B、C,所以他的偏好是 nr0,nr1,nr2
(使用候选人 [] 的索引)。然而,在你交换 A 和 C/nr 0 和 nr 2 之后(正如我原来的回答中提到的),第一个选民的偏好没有改变,仍然是:nr0, nr1,nr2
,但现在 nr0 是查理,不是安妮。所以这就像第一个选民投票 C,B,A
而不是 A,B,C
.
Charlie 不算在内,因为他现在已经被淘汰了,所以 nr1 (Bob) 获得了第一位投票者的选票。两个好的解决方案是在交换期间相应地更改偏好,或者根本不交换(我更喜欢)。
第一个解决方案:
int find_min(void)
{
int min = 0;
candidate tmp;
for (int i = 0; i < candidate_count - 1; i++)
{
for (int j = i + 1; j < candidate_count; j++)
{
if (candidates[j].votes < candidates[i].votes)
{
// swapping candidates
tmp = candidates[i];
candidates[i] = candidates[j];
candidates[j] = tmp;
//adjusting preferences
for(int a = 0; a < voter_count; a++) {
for(int b = 0; b < candidate_count; b++) {
if(preferences[a][b] == i) {
prefereences[a][b] = j;
}
else if (preferences[a][b] == j) {
prefereences[a][b] = i;
}
}
}
}
}
}
for (int k = 0; k < candidate_count; k++)
{
if (!candidates[k].eliminated)
{
min = candidates[k].votes;
break;
}
}
return min;
}
第二种解决方案(我认为更优雅,更快):
int find_min(void)
{
int min = voter_count;//the max poss value
for (int i = 0; i < candidate_count - 1; i++)
{
if (!candidates[i].eliminated) {
if (candidates[i].votes < min) {
min = candidates[i].votes;
}
}
}
return min;
}
编写这段代码我一直用 check50
检查我的程序,这些是我的结果:
:) runoff.c exists
:) runoff compiles
:) vote returns true when given name of candidate
:) vote returns false when given name of invalid candidate
:) vote correctly sets first preference for first voter
:) vote correctly sets third preference for second voter
:) vote correctly sets all preferences for voter
:) tabulate counts votes when all candidates remain in election
:) tabulate counts votes when one candidate is eliminated
:) tabulate counts votes when multiple candidates are eliminated
:) tabulate handles multiple rounds of preferences
:) print_winner prints name when someone has a majority
:) print_winner returns true when someone has a majority
:) print_winner returns false when nobody has a majority
:) print_winner returns false when leader has exactly 50% of vote
:) find_min returns minimum number of votes for candidate
:) find_min returns minimum when all candidates are tied
:) find_min ignores eliminated candidates
:) is_tie returns true when election is tied
:) is_tie returns false when election is not tied
:) is_tie returns false when only some of the candidates are tied
:) is_tie detects tie after some candidates have been eliminated
:) eliminate eliminates candidate in last place
:) eliminate eliminates multiple candidates in tie for last
:) eliminate eliminates candidates after some already eliminated
它似乎可以工作,但是当我用 cs50 示例测试它时,输出是不同的。 我该怎么办?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];
// Candidates have name, vote count, eliminated status
typedef struct
{
string name;
int votes;
bool eliminated;
}
candidate;
// Array of candidates
candidate candidates[MAX_CANDIDATES];
// Numbers of voters and candidates
int voter_count;
int candidate_count;
// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: runoff [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX_CANDIDATES)
{
printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
candidates[i].eliminated = false;
}
voter_count = get_int("Number of voters: ");
if (voter_count > MAX_VOTERS)
{
printf("Maximum number of voters is %i\n", MAX_VOTERS);
return 3;
}
// Keep querying for votes
for (int i = 0; i < voter_count; i++)
{
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
// Record vote, unless it's invalid
if (!vote(i, j, name))
{
printf("Invalid vote.\n");
return 4;
}
}
printf("\n");
}
// Keep holding runoffs until winner exists
while (true)
{
// Calculate votes given remaining candidates
tabulate();
// Check if election has been won
bool won = print_winner();
if (won)
{
break;
}
// Eliminate last-place candidates
int min = find_min();
bool tie = is_tie(min);
// If tie, everyone wins
if (tie)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
printf("%s\n", candidates[i].name);
}
}
break;
}
// Eliminate anyone with minimum number of votes
eliminate(min);
// Reset vote counts back to zero
for (int i = 0; i < candidate_count; i++)
{
candidates[i].votes = 0;
}
}
return 0;
}
// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
preferences[voter][rank] = i;
return true;
}
}
return false;
}
// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
for (int i = 0; i < voter_count; i++)
{
for (int j = 0; j < candidate_count; j++)
{
if (! candidates[preferences[i][j]].eliminated)
{
candidates[preferences[i][j]].votes++;
break;
}
}
}
}
// Print the winner of the election, if there is one
bool print_winner(void)
{
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > voter_count / 2)
{
printf("%s\n", candidates[i].name);
return true;
}
}
return false;
}
// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
int min = 0;
int x;
int y;
for (int i = 0; i < candidate_count - 1; i++)
{
for (int j = i + 1; j < candidate_count; j++)
{
if (candidates[j].votes < candidates[i].votes)
{
// swapping elements
x = candidates[i].eliminated;
candidates[i].eliminated = candidates[j].eliminated;
candidates[j].eliminated = x;
y = candidates[i].votes;
candidates[i].votes = candidates[j].votes;
candidates[j].votes = y;
}
}
}
for (int k = 0; k < candidate_count; k++)
{
if (!candidates[k].eliminated)
{
min = candidates[k].votes;
break;
}
}
return min;
}
// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
int ties = 0;
int elim = 0;
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
if (candidates[i].votes == min)
{
ties++;
}
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].eliminated)
{
elim++;
}
}
if (ties == candidate_count - elim)
{
return true;
}
return false;
}
// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
if (candidates[i].votes == min)
{
candidates[i].eliminated = true;
}
}
}
return;
}
例如使用这些输入:
./runoff Alice Bob Charlie
Number of voters: 5
Rank 1: Alice
Rank 2: Bob
Rank 3: Charlie
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Bob
Rank 2: Alice
Rank 3: Charlie
Rank 1: Charlie
Rank 2: Alice
Rank 3: Bob
程序应该 returns Alice
,但它 returns Bob
,我的代码有什么问题?
find_min 中的位交换候选不交换名称。
有问题:
用你的例子输入,在find_min()之前第一轮候选人[]处于以下状态:Alice,2;Bob,2;Charlie,1
(此时也到处都是假的,我在这里省略了)
find_min() 将其更改为 Alice,1;Bob2;Charlie,2
– 看看这里出了什么问题?查理犯下选举舞弊并进入下一次决选。
程序继续淘汰 Alice 并再次计票,Bob 获得 3 票,Charlie 获得 2 票(从那里开始它的工作与预期的差不多)。
排序时交换时出现错误。
编辑:
当您将候选人[i] 与候选人[j] 交换时(顺便说一句,您可以在 1 步而不是 3 步中使用 tmp 候选人),这会扰乱选民的偏好。
例如:第一个投票者正在投票给 A、B、C,所以他的偏好是 nr0,nr1,nr2
(使用候选人 [] 的索引)。然而,在你交换 A 和 C/nr 0 和 nr 2 之后(正如我原来的回答中提到的),第一个选民的偏好没有改变,仍然是:nr0, nr1,nr2
,但现在 nr0 是查理,不是安妮。所以这就像第一个选民投票 C,B,A
而不是 A,B,C
.
Charlie 不算在内,因为他现在已经被淘汰了,所以 nr1 (Bob) 获得了第一位投票者的选票。两个好的解决方案是在交换期间相应地更改偏好,或者根本不交换(我更喜欢)。
第一个解决方案:
int find_min(void)
{
int min = 0;
candidate tmp;
for (int i = 0; i < candidate_count - 1; i++)
{
for (int j = i + 1; j < candidate_count; j++)
{
if (candidates[j].votes < candidates[i].votes)
{
// swapping candidates
tmp = candidates[i];
candidates[i] = candidates[j];
candidates[j] = tmp;
//adjusting preferences
for(int a = 0; a < voter_count; a++) {
for(int b = 0; b < candidate_count; b++) {
if(preferences[a][b] == i) {
prefereences[a][b] = j;
}
else if (preferences[a][b] == j) {
prefereences[a][b] = i;
}
}
}
}
}
}
for (int k = 0; k < candidate_count; k++)
{
if (!candidates[k].eliminated)
{
min = candidates[k].votes;
break;
}
}
return min;
}
第二种解决方案(我认为更优雅,更快):
int find_min(void)
{
int min = voter_count;//the max poss value
for (int i = 0; i < candidate_count - 1; i++)
{
if (!candidates[i].eliminated) {
if (candidates[i].votes < min) {
min = candidates[i].votes;
}
}
}
return min;
}