Tideman 中的投票功能(CS50 的 pset 3)
Vote function in Tideman (pset 3 of CS50)
我正在尝试处理投票功能,有两个问题想寻求您的帮助:
- 在投票函数定义中,我们有:
bool vote(int rank, string name, int ranks[])
我不明白 rank 参数是干什么用的,为什么要在这里声明?
- 我对投票功能的解决方案如下:
// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
for (int j = 0; j < candidate_count; j++)
{
for (int k = 0; k < candidate_count; k++)
{
//Compare the name provided by the user with the name of the candidates numbered jth in the array candidates[MAX] which already populated above
if (strcmp(name, candidates[k]) == 0)
{
ranks[j] = k;
printf("ranks[%d] = %d\n", j, k);
}
}
return true;
}
return false;
}
printf函数的结果如下(with candidates = {a,b,c}, voter_count = 2):
等级 1:一个,
等级[0] = 0;
等级 2:b,
等级[0] = 1;
等级 3:c,
等级[0] = 2;
等级 1:c,
等级[0] = 2;
等级 2:b,
等级[0] = 1;
等级 3:一个,
排名[0] = 0
ranks[j] 中 j 的值未更新。我该如何解决这个问题?
非常感谢您的帮助!
这是一些代码:
// Update ranks given a new vote
bool vote(int rank, string name, int ranks[]){
//We want to cycle through the list of candidates given
for(int i = 0; i < candidate_count; i++){
//If the candidate(s) in the array matches with string name, we will continue
if(strcmp(candidates[i], name) == 0){
//This is the tricky part to understand. Read below for answer.
ranks[rank] = i;
return true;
}
}
return false;
}
int rank
表示用户给候选人的排名,int i
表示候选人在 candidates[]
中的位置。我们想根据正确的排名更新 ranks[]
。这仍然很难理解,所以这里有一个例子。
我们有四位候选人:John、Jim、Sam、Alex
In string candidates[MAX];
, John is at candidates[0]
, Jim is at candidates[1]
, Sam is at candidates[2]
, and Alex is at candidates[3]
.
假设用户投票并按以下顺序投票:
- 亚历克斯
- 约翰
- 吉姆
- 萨姆
让我们 运行 它在 bool vote(int rank, string name, int ranks[])
.
vote(j, name, ranks)
where j = 0, name = Alex, and ranks is the ranks[]
- We will loop the name Alex until we find it in
candidates[MAX]
.
- Alex is found in
candidates[i]
where i = 3.
- We want to update the
ranks[]
ranks[rank] = i;
would mean that at ranks[rank]
is equal to i which is 3. In other words, ranks[0] is equal to Alex's ith position in the candidates[MAX]
.
然后你重复这个循环,直到完成所有选民的排名。
我正在尝试处理投票功能,有两个问题想寻求您的帮助:
- 在投票函数定义中,我们有:
bool vote(int rank, string name, int ranks[])
我不明白 rank 参数是干什么用的,为什么要在这里声明?
- 我对投票功能的解决方案如下:
// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
for (int j = 0; j < candidate_count; j++)
{
for (int k = 0; k < candidate_count; k++)
{
//Compare the name provided by the user with the name of the candidates numbered jth in the array candidates[MAX] which already populated above
if (strcmp(name, candidates[k]) == 0)
{
ranks[j] = k;
printf("ranks[%d] = %d\n", j, k);
}
}
return true;
}
return false;
}
printf函数的结果如下(with candidates = {a,b,c}, voter_count = 2):
等级 1:一个, 等级[0] = 0; 等级 2:b, 等级[0] = 1; 等级 3:c, 等级[0] = 2; 等级 1:c, 等级[0] = 2; 等级 2:b, 等级[0] = 1; 等级 3:一个, 排名[0] = 0
ranks[j] 中 j 的值未更新。我该如何解决这个问题?
非常感谢您的帮助!
这是一些代码:
// Update ranks given a new vote
bool vote(int rank, string name, int ranks[]){
//We want to cycle through the list of candidates given
for(int i = 0; i < candidate_count; i++){
//If the candidate(s) in the array matches with string name, we will continue
if(strcmp(candidates[i], name) == 0){
//This is the tricky part to understand. Read below for answer.
ranks[rank] = i;
return true;
}
}
return false;
}
int rank
表示用户给候选人的排名,int i
表示候选人在 candidates[]
中的位置。我们想根据正确的排名更新 ranks[]
。这仍然很难理解,所以这里有一个例子。
我们有四位候选人:John、Jim、Sam、Alex
In
string candidates[MAX];
, John is atcandidates[0]
, Jim is atcandidates[1]
, Sam is atcandidates[2]
, and Alex is atcandidates[3]
.
假设用户投票并按以下顺序投票:
- 亚历克斯
- 约翰
- 吉姆
- 萨姆
让我们 运行 它在 bool vote(int rank, string name, int ranks[])
.
vote(j, name, ranks)
where j = 0, name = Alex, and ranks is the ranks[]- We will loop the name Alex until we find it in
candidates[MAX]
.- Alex is found in
candidates[i]
where i = 3.- We want to update the
ranks[]
ranks[rank] = i;
would mean that atranks[rank]
is equal to i which is 3. In other words, ranks[0] is equal to Alex's ith position in thecandidates[MAX]
.
然后你重复这个循环,直到完成所有选民的排名。