原文:

考虑到总数。任务是找出两个骰子掷 n 次的和出现的概率。 概率被定义为对结果总数有利的结果数。概率总是介于 0 和 1 之间。 示例:

input: sum = 11, times = 1
output: 2 / 36
favorable outcomes = (5, 6) and (6, 5) i.e 2
total outcomes = (1, 1), (1, 2), (1, 3)...(6, 6) i.e 36
probability = (2 / 36)
input: sum = 7, times = 7
output: 1 / 279936

公式:-

掷出 2 个骰子 n 次出现和的概率= (有利/总)^ n

进场:-

首先,计算 1 次掷出 2 个骰子的总和出现的概率。 假设概率 1。 现在,计算两个骰子掷 n 次之和出现的概率为: 概率 2 =(概率 1) ^ n. 即概率 1 升 n 次方

以下是上述方法的实施:

c

// c   implementation of above approach
#include 
using namespace std;
// function that calculates probability.
int probability(int sum, int times)
{
    float favorable = 0.0, total = 36.0;
    long int probability = 0;
    // to calculate favorable outcomes
    // in thrown of 2 dices 1 times.
    for (int i = 1; i <= 6; i  ) {
        for (int j = 1; j <= 6; j  ) {
            if ((i   j) == sum)
                favorable  ;
        }
    }
    int gcd1 = __gcd((int)favorable, (int)total);
    // reduce to simplest form.
    favorable = favorable / (float)gcd1;
    total = total / (float)gcd1;
    // probability of occurring sum on 2 dice n times.
    probability = pow(total, times);
    return probability;
}
// driver code
int main()
{
    int sum = 7, times = 7;
    cout << "1"
         << "/" << probability(sum, times);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of above approach
import java.io.*;
class gfg
{
// recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
    // everything divides 0
    if (a == 0)
    return b;
    if (b == 0)
    return a;
    // base case
    if (a == b)
        return a;
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
    return __gcd(a, b - a);
}
// function that calculates
// probability.
static long probability(int sum,
                        int times)
{
    float favorable = 0, total = 36;
    long probability = 0;
    // to calculate favorable outcomes
    // in thrown of 2 dices 1 times.
    for (int i = 1; i <= 6; i  )
    {
        for (int j = 1; j <= 6; j  )
        {
            if ((i   j) == sum)
                favorable  ;
        }
    }
    int gcd1 = __gcd((int)favorable,
                     (int)total);
    // reduce to simplest form.
    favorable = favorable / (float)gcd1;
    total = total / (float)gcd1;
    // probability of occurring
    // sum on 2 dice n times.
    probability = (long)math.pow(total, times);
    return probability;
}
// driver code
public static void main (string[] args)
{
    int sum = 7, times = 7;
    system.out.println( "1"   "/"  
          probability(sum, times));
}
}
// this code is contributed
// by inder_verma

python 3

# python 3 implementation of above approach
# from math import everything
from math import *
# function that calculates probability.
def probability(sum, times) :
    favorable, total, probability = 0.0, 36.0, 0
    # to calculate favorable outcomes
    # in thrown of 2 dices 1 times.
    for i in range(7) :
        for j in range(7) :
            if ((i   j) == sum) :
                favorable  = 1
    gcd1 = gcd(int(favorable), int(total))
    # reduce to simplest form.
    favorable = favorable / gcd1
    total = total / gcd1
    # probability of occurring sum on 2 dice n times.
    probability = pow(total, times)
    return int(probability)
# driver code
if __name__ == "__main__" :
    sum, times = 7, 7
    print("1","/",probability(sum, times))
# this code is contributed by ankitrai1

c

// c# implementation of above approach
class gfg
{
// recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
    // everything divides 0
    if (a == 0)
    return b;
    if (b == 0)
    return a;
    // base case
    if (a == b)
        return a;
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
    return __gcd(a, b - a);
}
// function that calculates
// probability.
static long probability(int sum,
                        int times)
{
    float favorable = 0, total = 36;
    long probability = 0;
    // to calculate favorable outcomes
    // in thrown of 2 dices 1 times.
    for (int i = 1; i <= 6; i  )
    {
        for (int j = 1; j <= 6; j  )
        {
            if ((i   j) == sum)
                favorable  ;
        }
    }
    int gcd1 = __gcd((int)favorable,
                    (int)total);
    // reduce to simplest form.
    favorable = favorable / (float)gcd1;
    total = total / (float)gcd1;
    // probability of occurring
    // sum on 2 dice n times.
    probability = (long)system.math.pow(total, times);
    return probability;
}
// driver code
public static void main()
{
    int sum = 7, times = 7;
    system.console.writeline( "1"   "/"  
        probability(sum, times));
}
}
// this code is contributed
// by mits

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

1/279936