【问题标题】:Voting program should eliminate lowest candidate and add votes to another. Java投票程序应淘汰最低的候选人并为另一个候选人增加选票。爪哇
【发布时间】:2016-04-06 23:34:25
【问题描述】:

我创建了一个程序,该程序读取一个文本文件并为候选人创建一个数组,然后为每个选民创建一个对象以及他们偏好的数组。我遇到问题的方法是投票方法。要使候选人获胜,他们必须获得超过一半的选票。因此,一种方法检查他们是否这样做,然后删除投票数最低的候选人,我遇到的问题是在删除它之后我不知道如何检查他们的下一个偏好。

这是我的投票方法

 public static int[] doVoting(Voter[] voters, String[] cand){

 // Create an int array to store votes
    int[] votes= new int[cand.length];


    //The first string of the candidates array will correspond to the first number in the int array

    for (int i=0; i< voters.length; i++){  
        for (int j=0; j< cand.length; j++){
            for (int k=0;k< voters[i].preferences.length; k++){
                if(voters[i].preferences[k].equals(cand[j]))
                {votes[j]= votes[j] + 1;
                }
                                    else 
                break;
            }
        }
    }

    return votes;
}

这里是它检查选票并做出最终决定的地方

public static void doAlternativeVoteElection(Voter[] voters, String[] candidate)
{
    int[] votes = doVoting(voters,candidate);

    int max = votes[0];
    int min = votes[0];
    int sumVote = 0;
    int elemax=0;
    int elemin=0;

    for (int i=0;i<votes.length; i++){
        sumVote += votes[i];
        if (votes[i] > max){
            max=votes[i];
            elemax = i;
        }
        if(votes[i] <= min){
            elemin = i;
            min= votes[i];
        }
    }

    while (max <= sumVote/2)
    {

        candidate = arrayDel(elemin, candidate);
        System.out.println("he");
        votes= arrayDelInt(elemin, votes);
        votes = doVoting(voters, candidate);

        for (int i=0; i<candidate.length;i++){
            System.out.println(candidate[i]);
            System.out.println(votes[i]);
            if (votes[i]> max){
                max=votes[i];
                elemax = i;
            }
            else if(votes[i] <= min){
                elemin = i;
                min= votes[i];
            }
            else
                continue;


        }

        if(max > sumVote/2)
            break;
    }
    if(max > sumVote/2)
        System.out.println(candidate[elemax]+" wins");
}


public static int[] arrayDelInt(int min, int[] array)
{
    int[] retva = new int[array.length-1];


    for (int i=0; i<min; i++)
        retva[i] = array[i];

    for (int i=min+1; i<array.length; i++)
        retva[i-1] = array[i];


    return retva; 

}

问题是 doVoting 方法不会在一个候选人被淘汰后将投票添加到下一个偏好。所以最终 arrayDel 方法只是删除所有元素。


这里是arrayDel方法

    public static int[] arrayDelInt(int min, int[] array)
{
    int[] retva = new int[array.length-1];


    for (int i=0; i<min; i++)
        retva[i] = array[i];

    for (int i=min+1; i<array.length; i++)
        retva[i-1] = array[i];


    return retva; 

}

arrayDelInt 也一样

【问题讨论】:

  • 你也可以发布'arrayDel()'和'arrayDelInt()'方法吗?这是作业吗?您是否仅限于使用数组?该任务的预期运行时间、内存消耗是多少?
  • 是的,这是作业,它只需要使用数组,而那些运行时间和内存无关紧要。
  • 为什么不创建一个包含“选民”对象数组的“候选人”类。它消耗大量内存,但在最坏的情况下,您可以在线性时间内找到最低投票的候选人,并且您不必更新最大值、最小值或类似的东西,只需选择下一个最低投票的“候选人”。

标签: java arrays


【解决方案1】:

您有两个可能是问题的潜在错误。

while (max &lt;= sumVote/2) sumVote/2 向下舍入。请改用sumVote / 2.0。这可能不是它,但无论如何应该被认为是一个潜在的错误。

在 doVoting 的这段代码中,根据您声明的要求,相信 break 语句应该在候选人的投票计数增加后发生。尝试将最后一个 if 块更改为如下所示

        if(voters[i].preferences[k].equals(cand[j])) {              
            votes[j]= votes[j] + 1;
            break;
        }

【讨论】:

  • sumVote 在此将始终相同。永远是 12 点。
  • 那我误解了你的要求。在特定候选人获得 >= 50% 的选票之前,是否会进行第二轮投票?如果是这样,那么您必须在循环中运行 doVoting 并重新计算后将 sumVote 重置为 0,否则 (max &lt;= sumVote/2) 总是会解析为 true。
  • 所以应该发生的情况是,如果他们都没有超过一半的选票,则选票最少的候选人将被删除。投票给已删除候选人的人再次投票支持他们的下一个偏好。总票数永远不变
  • 当我这样做时,它只是将它们全部设置为 12
【解决方案2】:

我认为最好重新考虑您的设计,使其更加面向对象,例如; (请考虑这个伪代码)

public class Candidate {

    // maps the preference i.e. 1st, 2nd, etc. to votes cast
    private HashMap<String, int> votes = new HashMap<String, int>();

    public void addFirstPref() {
        votes.put("first", votes.get("first") + 1);
    }
    // and so on for addSecondPref() etc
}

public class Ballot {

    // maps preference to candidate
    HashMap<String, String> votes = new HashMap<String, String>();

    public String getFirstPref() {
        return votes.get("first");
    }
    // and so on for getSecondPref() etc.
}

public class Election {

    // populate a collection of candidates and ballots

    public void doVoting() {
        // count all the ballots
        for (Ballot ballot : ballotsColn) {
            candidatesColn.get(ballot.getFirstPref()).addFirstPref();
            // repeat for all preferences
        }

        // now do rounds until there's one left
        while (candidatesColn.size() > 1) {
            // find the candidate with the lowest first preferences
            // find all the ballots where that candidate is the first pref
            // iterate all those ballots, find the second pref candidate
            // add 1 vote to the first pref count of that candidate
            // remove the candidate from the collection
        }
    }
}

【讨论】:

  • 他只能使用数组。没有哈希图。
  • 这似乎更简单,不幸的是我们需要以某些方式使用某些方法
  • 啊,错过了这个要求。政治是如此不必要地复杂:)
  • 如果你使用 Candidate[] CandidateColnBallot[] ballotsColn 会不会离你现在所做的不远,除了它是对象数组而不是基元数组。类似地,HashMaps 可以变成数组; Candidate int[] votes 其中 votes[0] 是第一偏好等。而 Ballot String[] votes 其中 votes[0 ] 是得到第一偏好的候选人,等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
相关资源
最近更新 更多