📌  相关文章
📜  在两种不同类型的硬币中选择一个随机硬币后获得两个连续正面的可能性

📅  最后修改于: 2021-05-08 18:39:02             🧑  作者: Mango

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

方法:
由于两个硬币都不相同,因此将使用贝叶斯定理来获得所需的概率。
由于硬币是随机选择的,因此可以选择它们中的任何一个,因此pq将包括在计算中。应用贝叶斯定理后,所需的答案将是(p * p + q * q)/(p + q),因为如果选择了第一个硬币,则使两个头背对背的概率为p * p,并且对于第二枚硬币。
这是贝叶斯定理的一个应用。

下面是上述方法的实现:

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..


PHP


Javascript


输出:
0.666666666666667