C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C++で2つのダイスをN回投げたときに合計を得る確率


サイコロのペアが入力として投げられた合計と回数が与えられます。タスクは、サイコロのペアをN回投げたときに与えられた合計を得る確率を決定することです。

確率は、利用可能なデータのセットから目的の出力を取得する可能性です。確率の範囲は0から1の間にあり、整数0は不可能の可能性を示し、1は確実性を示します。

Input-: sum = 12, N = 1
Output-: Probability = 1/36
Explanation-: if a pair of dice is thrown once then the combinations will be
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4),
(2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2),
(4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),
(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6). Out of these combinations we can
get sum 12 at pair (6, 6) only therefore probability would be 1/36

Input-: sum = 4 and N = 6
Output-: probability is : 1/2985984

以下のプログラムで使用されるアプローチは次のとおりです

  • サイコロが投げられた回数を示す合計とNの値を入力します
  • 2つのダイスをN回投げたときに合計が発生する確率を計算するには、次の式を適用します:(好ましい/合計)^ N
  • 次に、2つのサイコロを1回投げたときに、その合計が発生する確率を計算します。これは、sa1になります
  • 2つのサイコロをN回投げたときにその合計が発生する確率を計算するには、次のようになります-
  • Probability2 =(Probability 1)^ N.つまり、Probability1の累乗N

アルゴリズム

Start
Step 1-> declare function to calculate the probability
   int probability(int sum, int times)
   Declare and set float res = 0.0 and total = 36.0
   Declare and set long int probab = 0
   Loop For i = 1 and i <= 6 and i++
      Loop For j = 1 and j <= 6 and j++
         IF ((i + j) = sum)
            Set res++
         End
      End
   End
   Declare and set int gcd1 = __gcd((int)res, (int)total)
   Declare and set res = res / (float)gcd1
   Set total = total / (float)gcd1
   Set probab = pow(total, times)
   return probab
Step 2-> In main()
   Declare and set int sum = 4 and times = 6
   Call probability(sum, times)  
Stop

#include <bits/stdc++.h>
using namespace std;
// function that calculates the Probability of getting a sum on throwing 2 Dices N times
int probability(int sum, int times) {
   float res = 0.0, total = 36.0;
   long int probab = 0;
   for (int i = 1; i <= 6; i++) {
      for (int j = 1; j <= 6; j++) {
         if ((i + j) == sum)
         res++;
      }
   }
   int gcd1 = __gcd((int)res, (int)total);
   res = res / (float)gcd1;
   total = total / (float)gcd1;
   probab = pow(total, times);
   return probab;
}
int main() {
   int sum = 4, times = 6;
   cout<<"probability is : ";
   cout << "1" << "/" << probability(sum, times);
   return 0;
}

出力

probability is : 1/2985984

  1. C++のすべてのサブシーケンスの合計を求めます

    n個の要素を持つ配列Aがあるとします。配列のすべてのサブセットの合計の合計を見つける必要があります。したがって、配列がA =[5、6、8]の場合、-のようになります。 サブセット 合計 5 5 6 6 8 8 5,6 11 6,8 14 5,8 13 5,6,8 19 合計 76 配列にはn個の要素があるため、2n個のサブセット(空を含む)があります。はっきりと観察すると、各要素が2n〜1回発生していることがわかります 例 #include<iostream> #includ

  2. C ++の合計配列パズル?

    ここでは、配列に関連する1つの興味深い問題を確認します。 n個の要素を持つ配列があります。 n個の要素の別の配列を作成する必要があります。ただし、2番目の配列のi番目の位置は、i番目の要素を除く最初の配列のすべての要素の合計を保持します。そして、1つの制約は、この問題では減算演算子を使用できないことです。 減算演算を使用できれば、すべての要素の合計を取得し、最初の配列のi番目の要素を減算して、2番目の配列のi番目の場所に格納することで、この問題を簡単に解決できます。 ここでは、毎回要素を追加することでこれを解決し、0..n-1のiについては、位置iの要素を無視します。ポイントを得るためのア