我现在正在做CS50课程,我现在在做习题集3。我相信我的代码是正确的,但是这门课的check50,总是显示这个错误。我用项目描述中给出的例子进行了测试,得到了正确的答案。我错过什么了吗?
这是我的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedef struct
{
string name;
int votes;
}
candidate;
// Array of candidates
candidate candidates[MAX];
// Number of candidates
int candidate_count;
// Function prototypes
bool vote(string name);
void print_winner(void);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [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].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count = get_int("Number of voters: ");
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
bool check = false;
for (int j = 0; j < candidate_count; j++)
{
if (strcmp(candidates[j].name, name) == 0)
{
candidates[j].votes = candidates[j].votes + 1;
check = true;
break;
}
}
return check;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
int count = 0;
int equal_count = 0;
string s;
for (int k = 0; k < candidate_count - 1; k++)
{
if (candidates[k].votes > candidates[k + 1].votes)
{
count = candidates[k].votes;
}
else if (candidates[k + 1].votes > candidates[k].votes)
{
count = candidates[k + 1].votes;
}
else
{
equal_count = candidates[k + 1].votes;
}
}
if (equal_count > count)
{
count = equal_count;
}
for (int j = 0; j < candidate_count; j++)
{
if (candidates[j].votes == count)
{
s = candidates[j].name;
printf("%s\n", s);
}
}
return;
}
我得到了描述中的确切答案:
$ ./plurality Alice Bob Charlie
Number of voters: 4
Vote: Alice
Vote: Bob
Vote: Charlie
Vote: Alice
Alice
但这是我使用check50时得到的结果
:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:( print_winner identifies Alice as winner of election
print_winner function did not print winner of election
:) print_winner identifies Bob as winner of election
:) print_winner identifies Charlie as winner of election
:) print_winner prints multiple winners in case of tie
:) print_winner prints all names when all candidates are tied
check50总是显示此错误。
1条答案
按热度按时间a1o7rhls1#
您的
print_winner
函数不正确-您总是检查 * 相邻 * 候选项。但现在想象数到3,1,2!在你的循环中,你首先会发现3是3和1的最大值,但是在下一个循环中,你会发现2是1和2的最大值,然后相应地更新count
--这是不恰当的!相反,您需要找到一个全局最大值-因此将每个值与迄今为止找到的最大值进行比较。由于您需要至少一个候选人,您可以安全地执行以下操作:
现在你可以像以前一样安全地打印出自己的选票等于最大值的候选人。
旁注:
++someVariable
也可以工作,如果应用于结构体成员,所以你可以更方便地编写++candidates[j].votes
(在vote
函数中)-或者如果你愿意的话,后增量-就像你对循环变量所做的那样。但是,如果单独(即not in constructs likesomeArray[i++]
)我建议使用pre-increment --在C中没有太大的相关性,但是对于你可能会切换到C++的情况,请注意这些操作符可能会为结构/类重载,然后post-increment确实会产生性能开销。如果你从一开始就习惯了预增量,你就可以避免,即使留在C...