📜  在给出了击中目标的概率时,找出玩家获胜的概率

📅  最后修改于: 2021-04-29 15:50:21             🧑  作者: Mango

给定四个整数pqrs 。两个玩家正在玩一个游戏,其中两个玩家都击中目标,而第一个击中目标的玩家获胜,第一个玩家击中目标的概率为p / q ,而第二个玩家击中目标的概率为r /秒。任务是找到第一个玩家赢得游戏的可能性。

例子:

方法:第一个玩家击中目标的概率为p / q ,错过目标的概率为1 – p / q
第二名玩家击中目标的概率为r / s ,错过目标的概率为1 – r / s
假设第一个玩家为x ,第二个玩家为y
因此,总概率为x赢+(x输* y输* x赢)+(x输* y输* x输* y输* x赢)+…等等
因为x可以任意取胜,所以它是无限的序列。
t =(1 – p / q)*(1 – r / s) 。这里吨<1p / qr / s的总是<1。
因此,该级数将变为p / q +(p / q)* t +(p / q)* t 2 +…
这是一个无限GP系列,其公共比率小于1,其总和为(p / q)/(1-t)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the probability of the winner
double find_probability(double p, double q,
                        double r, double s)
{
  
    double t = (1 - p / q) * (1 - r / s);
  
    double ans = (p / q) / (1 - t);
  
    return ans;
}
  
// Driver Code
int main()
{
    double p = 1, q = 2, r = 1, s = 2;
  
    // Will print 9 digits after the decimal point
    cout << fixed << setprecision(9)
         << find_probability(p, q, r, s);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
import java.text.DecimalFormat;
  
class solution
{
  
// Function to return the probability of the winner
static double find_probability(double p, double q,
                        double r, double s)
{
  
    double t = (1 - p / q) * (1 - r / s);
  
    double ans = (p / q) / (1 - t);
  
    return ans;
}
  
// Driver Code
public static void main(String args[])
{
    double p = 1, q = 2, r = 1, s = 2;
  
    // Will print 9 digits after the decimal point
    DecimalFormat dec = new DecimalFormat("#0.000000000");
    System.out.println(dec.format(find_probability(p, q, r, s)));
}
}
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach 
  
# Function to return the probability
# of the winner 
def find_probability(p, q, r, s) :
      
    t = (1 - p / q) * (1 - r / s)
  
    ans = (p / q) / (1 - t); 
  
    return round(ans, 9)
  
# Driver Code 
if __name__ == "__main__" : 
  
    p, q, r, s = 1, 2, 1, 2
  
    # Will print 9 digits after 
    # the decimal point 
    print(find_probability(p, q, r, s)) 
  
# This code is contributed by Ryuga


C#
// C# mplementation of the approach
using System;
  
class GFG
{
  
// Function to return the probability of the winner
static double find_probability(double p, double q,
                        double r, double s)
{
  
    double t = (1 - p / q) * (1 - r / s);
  
    double ans = (p / q) / (1 - t);
  
    return ans;
}
  
// Driver Code
public static void Main()
{
    double p = 1, q = 2, r = 1, s = 2;
    Console.WriteLine(find_probability(p, q, r, s));
}
}
  
// This code is contributed by
// anuj_67..


PHP


输出:
0.666666667