Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找出仍有机会赢得选举的候选人人数_Java_Algorithm - Fatal编程技术网

Java 找出仍有机会赢得选举的候选人人数

Java 找出仍有机会赢得选举的候选人人数,java,algorithm,Java,Algorithm,选举正在进行中 考虑到目前为止给每个候选人的票数, 整数k表示尚未投票的选民人数, 找出仍有机会赢得选举的候选人人数 选举的获胜者必须获得比任何其他候选人都多的选票。 如果两名或两名以上候选人获得相同(最大)票数, 假设根本没有赢家 范例 对于票数=[2,3,5,2]和k=3,输出应为 选举获胜者(票数,k)=2。 int electionsWinners(int[] votes, int k) { int max = votes[0]; int counter = 0; /* I assu

选举正在进行中

考虑到目前为止给每个候选人的票数, 整数k表示尚未投票的选民人数, 找出仍有机会赢得选举的候选人人数

选举的获胜者必须获得比任何其他候选人都多的选票。 如果两名或两名以上候选人获得相同(最大)票数, 假设根本没有赢家

范例

对于票数=[2,3,5,2]和k=3,输出应为 选举获胜者(票数,k)=2。

int electionsWinners(int[] votes, int k) {

int max = votes[0];
int counter = 0;

/* I assumed all the voters who haven't cast their vote,
 votes only 1 candidate */

for(int i = 1; i < votes.length; i++) {
    //getting the candidate who has the highest vote.
    if(votes[i] > max ) {
        max = votes[i];
    }
}

// count the candidates who still have the chance to win.
for(int i = 0; i < votes.length; i++) {
    if(k != 0){
         if((votes[i] + k) > max) {
        counter++;
        }       
    } else if(k == 0) {
        /* if there is no voters left to vote, 
        and the candidates who has the highest vote will be the winner. 
        and if two or more candidates recieve the same(maximum) number of votes,
        assume there is no winner. */

        // count the no. of candidates who recieve the same(maximum) number of votes.
         if(votes[i] == max) {
        counter++;
             if(counter == 1) {
                 counter = 1;
             } else {
                 counter = 0;
             }
        } 
    }
}
return counter;
}
对于票数=[1,3,3,1,1]和k=0,输出应为 选举获胜者(票数,k)=0。

int electionsWinners(int[] votes, int k) {

int max = votes[0];
int counter = 0;

/* I assumed all the voters who haven't cast their vote,
 votes only 1 candidate */

for(int i = 1; i < votes.length; i++) {
    //getting the candidate who has the highest vote.
    if(votes[i] > max ) {
        max = votes[i];
    }
}

// count the candidates who still have the chance to win.
for(int i = 0; i < votes.length; i++) {
    if(k != 0){
         if((votes[i] + k) > max) {
        counter++;
        }       
    } else if(k == 0) {
        /* if there is no voters left to vote, 
        and the candidates who has the highest vote will be the winner. 
        and if two or more candidates recieve the same(maximum) number of votes,
        assume there is no winner. */

        // count the no. of candidates who recieve the same(maximum) number of votes.
         if(votes[i] == max) {
        counter++;
             if(counter == 1) {
                 counter = 1;
             } else {
                 counter = 0;
             }
        } 
    }
}
return counter;
}
对于票数=[5,1,3,1,4]和k=0,输出应为 选举获胜者(票数,k)=1。

int electionsWinners(int[] votes, int k) {

int max = votes[0];
int counter = 0;

/* I assumed all the voters who haven't cast their vote,
 votes only 1 candidate */

for(int i = 1; i < votes.length; i++) {
    //getting the candidate who has the highest vote.
    if(votes[i] > max ) {
        max = votes[i];
    }
}

// count the candidates who still have the chance to win.
for(int i = 0; i < votes.length; i++) {
    if(k != 0){
         if((votes[i] + k) > max) {
        counter++;
        }       
    } else if(k == 0) {
        /* if there is no voters left to vote, 
        and the candidates who has the highest vote will be the winner. 
        and if two or more candidates recieve the same(maximum) number of votes,
        assume there is no winner. */

        // count the no. of candidates who recieve the same(maximum) number of votes.
         if(votes[i] == max) {
        counter++;
             if(counter == 1) {
                 counter = 1;
             } else {
                 counter = 0;
             }
        } 
    }
}
return counter;
}
int选举获胜者(int[]票,int k){
int max=票数[0];
int计数器=0;
/*我假设所有没有投票的选民,
只投票给1名候选人*/
for(int i=1;imax){
max=投票数[i];
}
}
//数一数还有机会获胜的候选人。
for(int i=0;i最大值){
计数器++;
}       
}else如果(k==0){
/*如果没有选民投票,
得票最高的候选人将是赢家。
如果两名或两名以上候选人获得相同(最大)票数,
假设没有赢家*/
//计算获得相同(最大)票数的候选人人数。
如果(票数[i]==最大值){
计数器++;
如果(计数器==1){
计数器=1;
}否则{
计数器=0;
}
} 
}
}
返回计数器;
}

我是编程的初学者。尽我最大的努力解决它,这就是我的代码。我只是想知道是否有简化的解决方案。

我认为你的代码行不通,因为
k=0
,得票最多的候选人人数将等于
2*n+1
,例如
[1,3,3,3,1]

为什么?由于这一部分:

if(votes[i] == max) {
  counter++;
  if(counter == 1) {
     counter = 1;
  } else {
     counter = 0;
  }
}
因此,如果到目前为止你已经找到了两个得票最多的候选人,你就重置计数器。然后,当您找到另一个时,您将其增加到
1
,如果没有更多的候选人获得最大票数,您将返回
1
,这显然是不正确的

循环中的代码实际上应该是

...
} else if (k == 0 && votes[i] == max) {
  counter++;
}
for
循环之外,您可以进行快速检查

if (k == 0 && counter > 1)
  counter = 0;

首先,您当前的解决方案不正确。特别是《守则》

if (counter == 1) {
    counter = 1;
} ...
这很奇怪:若计数器等于1,那个么将其设置为1并没有效果。当
k==0
时发生的情况是:
计数器在0和1之间切换,这不是您想要的。用3名得票最多的候选人尝试你的解决方案

您正确地观察到有两种情况:if
k!=0
,您必须检查每个候选人是否会在所有剩余选民投票支持他/她时获胜。如果
k==0
您必须检查是否有一位候选人获得最高票数。因此,我希望您的代码看起来像以下伪代码:

determine maximum number of votes;

if (k != 0) {
   int counter = the number of candidates who win when all k votes are cast for him/her;
   return counter;
} else {
   int numOfWinners = the number of candidates with a maximum number of votes; 
   return (numOfWinners == 1) ? 1 : 0;
}

如果
语句过于冗长,我建议在
上发布,例如。g
if(k!=0)A else if(k==0)B
可以简化为
if(k!=0)A else B
<代码>如果(计数器==1)计数器=1是冗余的上次
否则
可以简化:
返回(…)?1:0
@SashaSalauyou。可以但这是伪代码。由于英文指令需要循环,我不想太依赖它们扮演表达式的角色。我现在明白了我的代码中的错误。谢谢@radoh