简体   繁体   English

CS50,复数问题,给出错误的结果

[英]CS50, Plurality question, gives the wrong result

Here is the plurality problem from CS50 I can't find fix the problem, please help这是CS50的多个问题我找不到解决问题,请帮助

PS I have another problem now, if there are 2 voters,1 is Alice,2nd is Bob the program should print out both as winners, but it gives out first candidate as a winner,how to deal with it? PS我现在有另一个问题,如果有2个选民,1是Alice,2nd是Bob,程序应该打印出两个作为获胜者,但是它给出了第一个候选人作为获胜者,如何处理它?

//Update vote totals given a new vote //根据新投票更新投票总数

bool vote(string name){
  for(int i=0;i<candidate_count; i++){
    if(strcmp(candidates[i].name,name)==0){
      candidates[i].vote++;
      return true;
    }
  }
  return false;
}

//Print the winner (ot winners)of the election //打印选举的获胜者(otwinnings)

void print_winner(void){
  int MaxVote=0;
  string Winner;
  for(int i=0;i<candidate_count; i++){
    if(candidates[i].vote>MaxVote){
      MaxVote=candidates[i].vote;
      Winner=candidates[i].name;
    }
  }
  for(int i=0;i<candidate_count; i++){
    if(candidates[i].vote==MaxVote){
      printf("%s\n",Winner);
      return;
    }
  }
}

Here is the output这是 output

   ~/pset3/plurality/ $ ./plurality Alice Bob
Number of voters: 2
Vote: Alice
Vote: Bob
Alice

I just finished this problem and what you need to do is print the first winner in your first "for" loop (in line 8 of your example) so it will print the first winner, then go back and print anyone who tied with them.我刚刚完成了这个问题,你需要做的是在你的第一个“for”循环中打印第一个获胜者(在你的例子的第 8 行),这样它将打印第一个获胜者,然后 go 返回并打印任何与他们并列的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM