原文:

给定两枚分别具有获得人头 p%q% 概率的硬币,任务是在给定硬币中随机选择硬币后,确定获得两个连续人头的概率。 例:

输入: p = 33,q = 66 输出:0.55000000000000 输入: p = 33,q = 66 输出:0.5500000000000

方法: 由于两个硬币不相同,因此将用于获得期望的概率。 由于硬币是随机选择的,因此可以选择其中任何一种,因此计算中将包括 pq 。应用贝叶斯定理后,需要的答案将是 (p * p q * q) / (p q) ,因为如果选择第一枚硬币,那么两个头像背对背的概率是 p * p ,第二枚硬币也是一样。 是贝叶斯定理的应用。

p(b | a)= p(a |^| b)/p(a)=(1/2 * p * p 1/2 * q * q)/(1/2 * p 1/2 * q)=(p * p q * q)/(p q)其中: p(b) =第二次投掷获得人头的概率, p(a) =第一次投掷获得人头的概率, p(a |^| b) =两次投掷都获得人头的概率。 所以,p(b | a)是第二次投掷获得人头的概率,前提是我们在第一次投掷中获得人头 。 此处 a、b 表示第 1、2 枚硬币。

以下是上述方法的实现:

c

// c   program to get the probability
// of getting two consecutive heads
#include 
using namespace std;
// function to return the probability
// of getting two consecutive heads
double getprobability(double p, double q)
{
    p /= 100;
    q /= 100;
    // formula derived from bayes's theorem
    double probability = (p * p   q * q) / (p   q);
    return probability;
}
// driver code
int main()
{
    double p, q;
    // given the probability of getting
    // a head for both the coins
    p = 80;
    q = 40;
    cout << fixed
         << setprecision(15)
         << getprobability(p, q)
         << endl;
    return 0;
}

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

// java program to get the probability
// of getting two consecutive heads
import java.io.*;
class gfg {
// function to return the probability
// of getting two consecutive heads
static double getprobability(double p, double q)
{
    p /= 100;
    q /= 100;
    // formula derived from bayes's theorem
    double probability = (p * p   q * q) / (p   q);
    return probability;
}
// driver code
    public static void main (string[] args) {
            double p, q;
    // given the probability of getting
    // a head for both the coins
    p = 80;
    q = 40;
     system.out.println( getprobability(p, q));
    }
}
// this code is contributed by  anuj_67..

python 3

# python 3 program to get the probability
# of getting two consecutive heads
# function to return the probability
# of getting two consecutive heads
def getprobability(p, q):
    p /= 100
    q /= 100
    # formula derived from bayes's theorem
    probability = (p * p   q * q) / (p   q)
    return probability
# driver code
if __name__ == "__main__":
    # given the probability of getting
    # a head for both the coins
    p = 80
    q = 40
    print(getprobability(p, q))
# this code is contributed
# by chitranayal

c

// c# program to get the probability
// of getting two consecutive heads
using system;
class gfg {
// function to return the probability
// of getting two consecutive heads
static double getprobability(double p, double q)
{
    p /= 100;
    q /= 100;
    // formula derived from bayes's theorem
    double probability = (p * p   q * q) / (p   q);
    return probability;
}
// driver code
    public static void main () {
            double p, q;
    // given the probability of getting
    // a head for both the coins
    p = 80;
    q = 40;
    console.writeline( getprobability(p, q));
    }
}
// this code is contributed by inder_verma..

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


java 描述语言


output: 

0.666666666666667