📜  当个人击中目标的概率给定时,A 赢得比赛的概率

📅  最后修改于: 2021-09-24 03:21:04             🧑  作者: Mango

给定四个整数abcd 。球员AB尝试点球。 A 击中目标的概率为a / b而 B 击中目标的概率为c / d 。先点球的玩家获胜。任务是找出 A 赢得比赛的概率。
例子:

方法:如果我们考虑变量K = a / b作为 A 射中目标的概率, R = (1 – (a / b)) * (1 – (c / d))作为 A 和 B 的概率都没有命中目标。
因此,该解形成几何级数K * R 0 + K * R 1 + K * R 2 + …..其总和为(K / 1 – R) 。将KR的值放入后,我们得到公式K * (1 / (1 – (1 – r) * (1 – k)))
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the probability of A winning
double getProbability(int a, int b, int c, int d)
{
 
    // p and q store the values
    // of fractions a / b and c / d
    double p = (double)a / (double)b;
    double q = (double)c / (double)d;
 
    // To store the winning probability of A
    double ans = p * (1 / (1 - (1 - q) * (1 - p)));
    return ans;
}
 
// Driver code
int main()
{
    int a = 1, b = 2, c = 10, d = 11;
    cout << getProbability(a, b, c, d);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to return the probability
// of A winning
static double getProbability(int a, int b,
                             int c, int d)
{
 
    // p and q store the values
    // of fractions a / b and c / d
    double p = (double) a / (double) b;
    double q = (double) c / (double) d;
 
    // To store the winning probability of A
    double ans = p * (1 / (1 - (1 - q) *
                               (1 - p)));
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a = 1, b = 2, c = 10, d = 11;
    System.out.printf("%.5f",
               getProbability(a, b, c, d));
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the probability
# of A winning
def getProbability(a, b, c, d) :
 
    # p and q store the values
    # of fractions a / b and c / d
    p = a / b;
    q = c / d;
     
    # To store the winning probability of A
    ans = p * (1 / (1 - (1 - q) * (1 - p)));
     
    return round(ans,5);
 
# Driver code
if __name__ == "__main__" :
 
    a = 1; b = 2; c = 10; d = 11;
    print(getProbability(a, b, c, d));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the probability
// of A winning
public static double getProbability(int a, int b,
                                    int c, int d)
{
 
    // p and q store the values
    // of fractions a / b and c / d
    double p = (double) a / (double) b;
    double q = (double) c / (double) d;
 
    // To store the winning probability of A
    double ans = p * (1 / (1 - (1 - q) *
                               (1 - p)));
    return ans;
}
 
// Driver code
public static void Main(string[] args)
{
    int a = 1, b = 2, c = 10, d = 11;
    Console.Write("{0:F5}",
                   getProbability(a, b, c, d));
}
}
 
// This code is contributed by Shrikant13


PHP


Javascript


输出:
0.52381