📜  掷N个骰子时获得所有可能值的可能性

📅  最后修改于: 2021-05-14 02:42:58             🧑  作者: Mango

给定一个表示骰子数量的整数N ,任务是找到将N个骰子放在一起可获得的每个可能值的概率。

例子:

方法:想法是使用动态编程和DP表存储每个可能值的概率。

  • 存储掷1个骰子时可能出现的所有6个数字的概率。
  • 现在,对于N = 2,[2,12]之间所有可能的和的概率等于两个数字相加后得出的概率之和。例如,
  • 因此,对于N个骰子,
  • 因此,为了解决该问题,我们需要使用以下关系,使用自顶向下方法将dp [] []表从2填充到N:
  • 显示为N存储的所有概率作为答案。

下面是上述方法的实现:

C++
// C++ Program to calculate
// the probabilty of
// all the possible values
// that can be obtained
// throwing N dices
 
#include 
using namespace std;
 
void dicesSum(int n)
{
    // Store the probablities
    vector > dp(n + 1);
    // Precompute the probabilities
    // for values possible using 1 dice
    dp[1] = { { 1, 1 / 6.0 },
              { 2, 1 / 6.0 },
              { 3, 1 / 6.0 },
              { 4, 1 / 6.0 },
              { 5, 1 / 6.0 },
              { 6, 1 / 6.0 } };
 
    // Compute the probabilies
    // for all values from 2 to N
    for (int i = 2; i <= n; i++) {
        for (auto a1 : dp[i - 1]) {
            for (auto a2 : dp[1]) {
                dp[i][a1.first + a2.first]
                    += a1.second * a2.second;
            }
        }
    }
    // Print the result
    for (auto a : dp[n]) {
        cout << a.first << " "
             << setprecision(2)
             << a.second
             << endl;
    }
}
 
// Driver code
int main()
{
    int n = 2;
    dicesSum(n);
 
    return 0;
}


Java
// Java program to calculate
// the probabilty of all the
// possible values that can
// be obtained throwing N dices
import java.io.*;
import java.util.*;
 
class GFG{
 
static void dicesSum(int n)
{
     
    // Store the probablities
    double[][] dp = new double[n + 1][6 * n + 1];
 
    // Precompute the probabilities
    // for values possible using 1 dice
    for(int i = 1; i <= 6; i++)
        dp[1][i] = 1 / 6.0;
 
    // Compute the probabilies
    // for all values from 2 to N
    for(int i = 2; i <= n; i++)
        for(int j = i - 1; j <= 6 * (i - 1); j++)
            for(int k = 1; k <= 6; k++)
            {
                dp[i][j + k] += (dp[i - 1][j] *
                                 dp[1][k]);
            }
 
    // Print the result
    for(int i = n; i <= 6 * n; i++)
    {
        System.out.println(i + " " +
                           Math.round(dp[n][i] * 1000.0) /
                                                 1000.0);
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 2;
     
    dicesSum(n);
}
}
 
// This code is contributed by jithin


Python3
# Python3 program to calculate
# the probabilty of all the
# possible values that can
# be obtained throwing N dices
def diceSum(n):
     
    # Inititalize a 2d array upto
    # (n*total sum possible) sum
    # with value 0
    dp = [[ 0 for j in range(n * 6)]
              for i in range(n + 1)]
                   
    # Store the probability in a
    # single throw for 1,2,3,4,5,6
    for i in range(6):
        dp[1][i] = 1 / 6
         
    # Compute the probabilies
    # for all values from 2 to N
    for i in range(2, n + 1):
        for j in range(len(dp[i - 1])):
            for k in range(6):
                     
                if (dp[i - 1][j] != 0 and
                    dp[i - 1][k] != 0):
                    dp[i][j + k] += (dp[i - 1][j] *
                                     dp[1][k])
     
    # Print the result
    for i in range(len(dp[n]) - n + 1):
        print("%d %0.3f" % (i + n, dp[n][i]))
 
# Driver code
n = 2
 
# Call the function
diceSum(n)
 
# This code is contributed by dipesh99kumar


C#
// C# program to calculate
// the probabilty of all the
// possible values that can
// be obtained throwing N dices
using System;
class GFG {
     
    static void dicesSum(int n)
    {
          
        // Store the probablities
        double[,] dp = new double[n + 1,6 * n + 1];
      
        // Precompute the probabilities
        // for values possible using 1 dice
        for(int i = 1; i <= 6; i++)
            dp[1,i] = 1 / 6.0;
      
        // Compute the probabilies
        // for all values from 2 to N
        for(int i = 2; i <= n; i++)
            for(int j = i - 1; j <= 6 * (i - 1); j++)
                for(int k = 1; k <= 6; k++)
                {
                    dp[i,j + k] += (dp[i - 1,j] *
                                     dp[1,k]);
                }
      
        // Print the result
        for(int i = n; i <= 6 * n; i++)
        {
            Console.WriteLine(i + " " +
                               Math.Round(dp[n,i] * 1000.0) /
                                                     1000.0);
        }
    }
 
  static void Main() {
    int n = 2;
  
    dicesSum(n);
  }
}
 
// This code is contributed by divyesh072019


输出:
2 0.028
3 0.056
4 0.083
5 0.11
6 0.14
7 0.17
8 0.14
9 0.11
10 0.083
11 0.056
12 0.028

时间复杂度: O(N 2 )
辅助空间: O(N 2 )