📜  当个别机率达到指定目标时,A赢得比赛的机率

📅  最后修改于: 2021-06-26 17:14:35             🧑  作者: 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


输出:
0.52381

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。