原文:
给定奇数长度的数组p[]n,其中 p[i] 表示获得第i枚硬币正面的概率。由于硬币有偏差,获得人头的概率并不总是等于 0.5 。任务是找到头部比尾部获得更多次数的概率。
示例:
输入: p[] = {0.3,0.4,0.7 } t3】输出: 0.442 一个尾巴的概率=(1–一个头部的概率) 对于头部大于尾部,有 4 种可能: t8】p({头部,头部,尾部})= 0.3 x 0.4 x(1–0.7)= 0.036 p({尾部,头部,头部 head })= 0.3 x(1–0.4)x 0.7 = 0.126 p({ head,head,head })= 0.3 x 0.4 x 0.7 = 0.084 将上述概率 相加为 0.036 0.196 0.126 0.084 = 0.442
输入: p[] = {0.3,0.5,0.2,0.6,0.9 } t3】输出: 0.495
天真的方法:天真的方法是创造所有正面和反面的2nt5】可能性。然后计算不同排列的概率,当头部的数量大于尾部的数量时相加,就像例子解释的那样。当 n 较大时,这将给出 tle。
高效途径:思路是使用。让我们假设 dp[i][j] 是用第一枚 i 硬币获得 j 人头的概率。要将 j 头放在 i th 位置,有两种可能:
- 如果直到(i–1)硬币的头数等于 j ,那么在 i th 处会出现一条尾巴。
- 如果直到(i–1)硬币的头数等于(j–1),则一个头出现在 i 第 位置
因此,它可以分成如下子问题:
dp[i][j] = dp[i – 1][j] * (1 – p[i]) dp[i – 1][j – 1] * p[i]
下面是上述方法的实现:
c
// c implementation of the above approach
#include
using namespace std;
// function to return the probability when
// number of heads is greater than the number of tails
double probability(double p[], int n)
{
// declaring the dp table
double dp[n 1][n 1];
memset(dp, 0.0, sizeof(dp));
// base case
dp[0][0] = 1.0;
// iterating for every coin
for (int i = 1; i <= n; i = 1) {
// j represents the numbers of heads
for (int j = 0; j <= i; j = 1) {
// if number of heads is equal to zero
// there there is only one possibility
if (j == 0)
dp[i][j] = dp[i - 1][j]
* (1.0 - p[i]);
else
dp[i][j] = dp[i - 1][j]
* (1.0 - p[i])
dp[i - 1][j - 1] * p[i];
}
}
double ans = 0.0;
// when the number of heads is greater than (n 1)/2
// it means that heads are greater than tails as
// no of tails no of heads is equal to n for
// any permutation of heads and tails
for (int i = (n 1) / 2; i <= n; i = 1)
ans = dp[n][i];
return ans;
}
// driver code
int main()
{
// 1 based indexing
double p[] = { 0.0, 0.3, 0.4, 0.7 };
// number of coins
int n = sizeof(p) / sizeof(p[0]) - 1;
// function call
cout << probability(p, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the above approach
class gfg
{
// function to return the probability when
// number of heads is greater than the number of tails
static double probability(double p[], int n)
{
// declaring the dp table
double [][]dp = new double[n 1][n 1];
// base case
dp[0][0] = 1.0;
// iterating for every coin
for (int i = 1; i <= n; i = 1)
{
// j represents the numbers of heads
for (int j = 0; j <= i; j = 1)
{
// if number of heads is equal to zero
// there there is only one possibility
if (j == 0)
dp[i][j] = dp[i - 1][j]
* (1.0 - p[i]);
else
dp[i][j] = dp[i - 1][j]
* (1.0 - p[i])
dp[i - 1][j - 1] * p[i];
}
}
double ans = 0.0;
// when the number of heads is greater than (n 1)/2
// it means that heads are greater than tails as
// no of tails no of heads is equal to n for
// any permutation of heads and tails
for (int i = (n 1) / 2; i <= n; i = 1)
ans = dp[n][i];
return ans;
}
// driver code
public static void main(string[] args)
{
// 1 based indexing
double p[] = { 0.0, 0.3, 0.4, 0.7 };
// number of coins
int n = p.length - 1;
// function call
system.out.println(probability(p, n));
}
}
// this code is contributed by rajput-ji
python 3
# python3 implementation of the above approach
import numpy as np
# function to return the probability when
# number of heads is greater than
# the number of tails
def probability(p, n) :
# declaring the dp table
dp = np.zeros((n 1, n 1));
for i in range(n 1) :
for j in range(n 1) :
dp[i][j] = 0.0
# base case
dp[0][0] = 1.0;
# iterating for every coin
for i in range(1, n 1) :
# j represents the numbers of heads
for j in range(i 1) :
# if number of heads is equal to zero
# there there is only one possibility
if (j == 0) :
dp[i][j] = dp[i - 1][j] * (1.0 - p[i]);
else :
dp[i][j] = (dp[i - 1][j] * (1.0 - p[i])
dp[i - 1][j - 1] * p[i]);
ans = 0.0;
# when the number of heads is greater than (n 1)/2
# it means that heads are greater than tails as
# no of tails no of heads is equal to n for
# any permutation of heads and tails
for i in range((n 1)// 2, n 1) :
ans = dp[n][i];
return ans;
# driver code
if __name__ == "__main__" :
# 1 based indexing
p = [ 0.0, 0.3, 0.4, 0.7 ];
# number of coins
n = len(p) - 1;
# function call
print(probability(p, n));
# this code is contributed by ankitrai01
c
// c# implementation of the above approach
using system;
class gfg
{
// function to return the probability when
// number of heads is greater than the number of tails
static double probability(double []p, int n)
{
// declaring the dp table
double [,]dp = new double[n 1, n 1];
// base case
dp[0, 0] = 1.0;
// iterating for every coin
for (int i = 1; i <= n; i = 1)
{
// j represents the numbers of heads
for (int j = 0; j <= i; j = 1)
{
// if number of heads is equal to zero
// there there is only one possibility
if (j == 0)
dp[i,j] = dp[i - 1,j]
* (1.0 - p[i]);
else
dp[i,j] = dp[i - 1,j]
* (1.0 - p[i])
dp[i - 1,j - 1] * p[i];
}
}
double ans = 0.0;
// when the number of heads is greater than (n 1)/2
// it means that heads are greater than tails as
// no of tails no of heads is equal to n for
// any permutation of heads and tails
for (int i = (n 1) / 2; i <= n; i = 1)
ans = dp[n,i];
return ans;
}
// driver code
static public void main ()
{
// 1 based indexing
double []p = { 0.0, 0.3, 0.4, 0.7 };
// number of coins
int n = p.length - 1;
// function call
console.write(probability(p, n));
}
}
// this code is contributed by ajit.
java 描述语言
output:
0.442
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处