2016-04-06 5 views
0

私は、テキストファイルを読み込んで候補者のための配列を作成し、次に各投票者のためのオブジェクトをその好みの配列と共に作成しました。私が問題を抱えている方法は、投票方法です。候補者が勝つためには、投票の半分以上が必要です。そこで、1つの方法で投票が行われているかどうかがチェックされ、投票数が最も少ない候補が削除されます。削除した後の問題は次の優先順位を確認する方法がわかりません。ここで投票プログラムは、最低の候補者を削除し、別の候補者に票を追加する必要があります。 Java

は、それが票をチェックし、最終決定

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; 

} 

を作るところ投票

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; 
} 

ここでは、問題がdoVotingメソッドが追加されないということであるん私の方法であり、 1人の候補が削除された後に次の嗜好に投票する。最終的に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

+2

'arrayDel()'と 'arrayDelInt()'メソッドも投稿できますか?これはとにかく宿題ですか?配列のみを使用するように制限されていますか?予想される実行時間、このタスクのメモリコストはいくらですか? –

+0

はいこれは宿題なので、配列を使用するだけで実行時間とメモリは問題になりません。 –

+0

「投票者」オブジェクトの配列を保持する「候補」クラスを作成しないのはなぜですか?それは多くのメモリを消費しますが、最悪の場合、最も低い投票者候補を線形時間内に見つけることができます。次の最も低い投票者の「候補者」を選ぶだけで、最大値も最小値も更新する必要もありません。 –

答えて

0

のためにその同じです。

while (max <= sumVote/2) < ---整数除算では、sumVote/2が切り捨てられます。代わりにsumVote/2.0を使用してください。これはおそらくそうではありませんが、とにかく潜在的なバグとみなされるべきです。

doVotingのこのコードでは、指定された要件を考慮して、候補者の投票数を増やした後にbreakステートメントを実行する必要があると考えています。最後にブロックを次のように変更してみてください

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

sumVoteはこれと常に同じです。それはいつも12になるでしょう。 –

+0

あなたの要件を誤解しています。特定の候補者が投票の50%以上を占めるまで、この流出は投票されますか?もしそうなら、ループ内でdoVotingを実行した後にsumVoteを0にリセットして再計算しなければなりません。そうしないと、 '(max <= sumVote/2)'は常にtrueに解決されます。 – whaley

+0

だから何が起きるべきかは、投票の半数以上の人が1人もいなくても、投票が最も少ない候補が削除された場合です。削除された候補者に投票した人々は、次の選好のために再び投票します。投票総数は決して変更されません –

0

もっとオブジェクト指向になるようにデザインを考え直す方がよいと思います。 (この擬似コードを考慮してください)

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 
     } 
    } 
} 
+0

彼は配列のみを使用する必要があります。ハッシュマップはありません。 –

+0

これは簡単な方法ですが、残念ながら特定の方法で特定の方法を使用する必要があります –

+0

ああ、その要件を逃した。政治がとても不必要に複雑になっています:) –

関連する問題