2016-08-16 7 views
1

グループのサイズが与えられたときに、グループ内の2人が同じ誕生日を持つ回数を計算しようとしています。私はまた、シミュレーションが何回実行されたかを教えています。私は、与えられた量のシミュレーションのうち、同じ誕生日を2人が共有している回数を正確に返そうとしています。 最初に配列を作成した後、メソッドを呼び出して要素をhashMapに入れ、hashMapに同じ値の2つがある場合に表示します。しかし、Android Studioで実行しているときに正しい割合を得られません。実際、私はパーセンテージ・オフになっています。また、このブロックの上にint型のグローバルスタティックマッチ変数を宣言しました。間違ったパーセンテージを返す

/** 
* sameBday: Create a word count mapping from an array 
*/ 
public void sameBday(int[] valueHolder) { 
    Map<Integer, Integer> myMap = new HashMap<Integer, Integer>(); 

    for(int number: valueHolder){ 
     if(!myMap.containsKey(number)){ 
      myMap.put(number, 1); 
     } 
     else if(myMap.containsKey(number)){ 
      myMap.put(number, myMap.get(number) + 1); 
      match++; 
      break; 
     } 
    } 
} 

public double calculate(int size, int count) { 
    double percentage = 0.0; 

    int[] myArray = new int[size]; 

    for(int i = 1; i <= count; i++){ 
     Random r = new Random(i); 
     for(int j = 0; j < size; j++){ 
      myArray[j] = r.nextInt(365) + 1; 
     } 

     sameBday(myArray); 

     if(i == count){ 
      percentage = (match * (100.0/i)); 
     } 
    } 
    return percentage; 
} 
+0

がよろしいのではない '割合は=(マッチ*(100.0 /(サイズ*数)))であり、' –

+1

はなぜあなたが出て壊しています'sameBday'のループの? – copeg

+0

私は2回のbdays(または同じ値のhashMapの値)を取得すると、hashMapの作成をやめ、calculateメソッドの次の繰り返しに移動したいので、ループを壊しています。 –

答えて

1

あなたのコードは奇妙なものでいっぱいですが、これはすべて問題ありません。最初のことはMapです。あなたはそれを必要としません。あなたはループのためにちょうどいい古いを作成することができます。また、同じ人物(これはi != jの状態です)を比較することはありませんが、実際に地図でこれを行いたい場合は、 )to mapキーの値が1より大きいかどうかをチェックし、trueの場合はマッチします。

ループの終わりに何かするには?

if(i == count){ 
    percentage = (match * (100.0/i)); 
} 

私はあなたを怒ら場合いいえ、ちょうどループの後にこれを行う:)

//At the beginning there is int match = 0; 

public void sameDayBirthday(int[] birthdays) { 
    for(int i = 0; i < birthdays.length; i++) { 
     for(int j = 0; j < birthdays.length; j++) { 
      if(birthdays[i] == birthdays[j] && i != j) { 
       match++; 
       return; 
      } 
     } 
    } 
} 

public double calculate(int size, int count) { 
    int[] birthdays = new int[size]; 
    Random r = new Random(); 

    for(int i = 1; i <= count; i++){ //looping through i counts (or 20 counts in this case 
     for(int j = 0; j < size; j++){ //looping through j times every i iteration 
      birthdays[j] = r.nextInt(365) + 1; 
     } 
     sameDayBirthday(birthdays); 
    } 
    return (match * (100.0/(double) count)); 

} 

このコードをcalculate(23, 1000000)を呼び出すことによって、私は申し訳ありませんが、私に22人の47.48690%

ための50.7685パーセントの確率で、しまいました私はそれを意味しませんでした。質問がある場合はコメントを残してください。

+0

ありがとうございました!ちょうどJavaで始まります。これは間違いなく実装が簡単です。 –

0

私はHashSetのを使用してsameBday機能をスキップします:

public double calculate(int size, int count) { 
    int match = 0; 
    Random r = new Random(); 

    for(int i = 1; i <= count; i++){ //looping through i counts (or 20 counts in this case 
     Set<Integer> birthdays = new HashSet<Integer>(size); 
     for(int j = 0; j < size; j++){ //looping through j times every i iteration 
      Integer birthday = r.nextInt(365) + 1; 
      if (birthdays.contains(birthday)) { 
       match++; 
       break; 
      } else { 
       birthdays.add(birthday); 
      } 
     } 
    } 

    return (match * (100.0/count)); 

} 
関連する問題