抛硬币正面期望_如果抛硬币,正面的数量多于反面的可能性

抛硬币正面期望

Problem statement:

问题陈述:

Let N be a positive odd number. There are N coins, numbered 1, 2 , ... , N. For each i (1≤i≤N), when Coin i is tossed, it comes up heads with probability pi and tails with probability 1-pi.

N为正奇数。 有N个硬币,编号为1,2,...,N 。 对于每个i ( 1≤i≤N ),当投入硬币i时,它的出现概率为pi出现在正面 ,出现的概率为1-pi出现在尾部。

Find the probability of having more heads than tails if coins are tossed.

找到抛硬币后正面多于尾的概率。

Input:

输入:

The first line contains N, number of coins. The second line contains the probability of coming head in the coins.

第一行包含N个硬币数量。 第二行包含进入硬币的概率。

Output:

输出:

Output the probability of having more number of heads than tails if coins are tossed.

如果抛硬币,则输出正面比背面多的概率。

Example with explanation:

带有说明的示例:

    Input: 
N=3
[0.30, 0.60, 0.80]
Output: 
0.612
The probability of having (Coin1,Coin2,Coin3) = (Head,Head,Head) 
is 0.3×0.6×0.8 = 0.144;
The probability of having (Coin1,Coin2,Coin3) = (Tail,Head,Head) 
is 0.7×0.6×0.8 = 0.336;
The probability of having (Coin1,Coin2,Coin3) = (Head,Tail,Head)
is 0.3×0.4×0.8 = 0.096;
The probability of having (Coin1,Coin2,Coin3) = (Head,Head,Tail)
is 0.3×0.6×0.2 = 0.036
Thus, the probability of having more heads than tails is 
0.144 + 0.336 + 0.096 + 0.036 = 0.612

Solution Approach

解决方法

Dynamic Programming

动态编程

Since the problem can be solved with recursion with time complexity of O(2^n) which is not accepted as there are other better approach to solve the problem with time complexity of O(n*n);

由于可以用O(2 ^ n)的时间复杂度递归来解决问题,这是不被接受的,因为还有其他更好的方法可以解决O(n * n)的时间复杂度问题;

Let's assume prob[i][j] to be the probability of getting j heads with first i coins. To get j heads at the ith position, there are two possibilities:

假设prob [i] [j]是第一个i个硬币获得j个头的概率。 为了使j个头位于第i 位置,有两种可能性:

If the number of heads till (i - 1) coins is equal to j then a tail comes at ith. If the number of heads till (i - 1) coins is equal to (j - 1) then a head comes at ith position.

如果直到(i-1)个硬币的正面数等于j, i 背面为尾数 。 如果直到(i-1)个硬币的正面数等于(j-1),则正面在 i 位置。

The probability of occurring jth head at ith coin toss depends on the number of heads in (i-1)th coins, if (i-1) coin have (j-1) head then jth coin occurs at ith coin(p[i]), and if (i-1) coins have already j coins then at jth toss tails come(1-p[i]).

第i个硬币抛掷第j个硬币的概率取决于第(i-1) 硬币的硬币的数目,如果(i-1)个硬币具有(j-1) 硬币的硬币,则第j个硬币出现在 i 硬币( p [ i] ),如果(i-1)个硬币已经有j个硬币,则第j个抛尾出现( 1-p [i] )。

Pseudo Code:

伪代码:

// p[] is the probability array,
// n is the size of array.
solve(p[],n):
// declare prob[n+1][n+1] array of having required head.
prob[n+1][n+1]  
// no head out of 1 coins
prob[1][0]=1-p[0] 
// one head out of one coins
prob[1][1]=p[0]	  
for(i=2;i<=n;i++)
// probability of having no heads.
prob[i][0]=prob[i-1][0]*(1-p[i])  
for(i=2;i<=n;i++)
for(j=1;j<=i;j++)
prob[i][j]=prob[i-1][j-1]*p[i-1]+prob[i-1][j]*(1-p[i-1])
// probability of having jth head can take 
// place in (i-1) coins or at (i)th coin.
sum=0
for(i=n/2+n%2;i<=n;i++)
//probability of heads>tails.
sum+=prob[n][i]   
return sum	

Time complexity: In worst case O(n*n)

时间复杂度 :在最坏的情况下O(n * n)

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
typedef double ll;
int main()
{
cout << "Enter number of test cases: ";
int t;
cin >> t;
while (t--) {
cout << "Enter the number of coins: ";
int n;
cin >> n;
ll p[n];
cout << "Enter the probabilities of head: ";
for (int i = 0; i < n; i++)
cin >> p[i];
ll prob[n + 1][n + 1];
// probability of one heads with one coins
prob[1][1] = p[0];
// probability of no heads with one coins
prob[1][0] = 1 - p[0];
for (int i = 2; i <= n; i++)
// probability of no heads with i coins.
prob[i][0] = prob[i - 1][0] * (1 - p[i - 1]);
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
// probability of head at ith coin it it occurs at(i-1)
// then just multiply with tail probability otherwise
// multiply with head probability.
prob[i][j] = prob[i - 1][j - 1] * p[i - 1] + prob[i - 1][j] * (1 - p[i - 1]);
}
ll sum = 0;
for (int i = n / 2 + n % 2; i <= n; i++)
// take those probability which have more heads than tails.
sum += prob[n][i];
cout << "The probability of heads more than the tails is: ";
cout << setprecision(10) << sum << "\n";
}
return 0;
}

Output

输出量

Enter number of test cases: 3
Enter the number of coins: 5
Enter the probabilities of head: 0.42 0.01 0.42 0.99 0.42
The probability of heads more than the tails is: 0.3821815872
Enter the number of coins: 3
Enter the probabilities of head: 0.30 0.60 0.80
The probability of heads more than the tails is: 0.612
Enter the number of coins: 1
Enter the probabilities of head: 0.50
The probability of heads more than the tails is: 0.5

翻译自: https://www.includehelp.com/icp/probability-of-getting-more-number-of-heads-than-tails-if-coins-are-tossed.aspx

抛硬币正面期望

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380593.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

委托用法

一、委托callback 回调函数声明委托&#xff1a;&#xff08;与声明类相似&#xff09;public delegate string MyDelegate(string sInput);使用委托&#xff1a;1. MyDelegate myDelegateCase new MyDelegate(InvokeMethod);private string InvokeMethod(string sInput){retu…

汇编程序中,字符数据和ASCII的对应关系

在汇编程序中&#xff0c;用’…的方式指明数据是以字符的形式给出的&#xff0c;编译器将它们转化为对应的ASCII码。 assume cs:code,ds:datadata segmentdb unIXdb foRK data endscode segmentstart: mov al,amov bl,bmov ax,4c00hint 21h code ends end startdb ‘unIX’ 相…

[转载]悖论在向mm表白中的应用

在日月光华BBS上看到的一个签名档我&#xff1a;我的第一个问题是&#xff0c;对於我第二个和第三个问题&#xff0c;你可不可以只用‘能’和‘不能’来回答mm&#xff1a;可以啊&#xff01;我&#xff1a;我的第二个问题是&#xff0c;如果我的第三个问题是你能不能做我的女朋…

A4Desk 网站破解

A4Desk是一个不错的Flash站点建站工具&#xff0c;不过生成的swf文件很不爽&#xff0c;主要是1、单击3次就会显示注册对话框&#xff1b;2、会在网站上显示Demo 字样。 如果希望去掉这些信息&#xff0c;按如下步骤操作即可&#xff1a; 一、用A4Desk建立站点并导出。 二、用S…

python嵩天第三章答案,Python语言程序设计嵩天答案

Python语言程序设计嵩天答案更多相关问题根据硬软酸碱理论&#xff0c;判断下列化合物哪些易溶于水。 CaI2&#xff0c; CaF2&#xff0c; PbCl2&#xff0c;PbCl4&#xff0c; CuCN&#xff0c; ZnSO4指出下列物质在液氨中的酸碱行为(写出反应方程式)&#xff1a; CH3COOH&…

[转]英语学习资源

Merriam-Webster Online: 快速查找单词意义&#xff0c;并有朗读。OneLook Dictionary Search: 搜索更多的字典&#xff0c;包括m-w.Pocket English Idioms Online: “A shot in the dark”是什么意思&#xff1f;And much more.Wordsmith A Word A Day: 适合钻研单词的朋友&am…

and和or指令

and指令&#xff1a;逻辑与指令&#xff0c;按位进行与运算 mov al,01100011B and al,00111011B执行后al00100011B&#xff0c;两个为1才为1&#xff0c;所以通过该指令可将操作数对象的相应位设为0&#xff0c;其他位不变 or指令&#xff1a;逻辑或指令&#xff0c;按位进行…

c 指针打印变量_C程序打印不同类型的指针变量的大小。

c 指针打印变量Any type of pointer variable takes the same memory bytes in the memory, because they are used to store the memory addresses on other type of variables. 任何类型的指针变量都在内存中占用相同的内存字节&#xff0c;因为它们用于在其他类型的变量上存…

strtus2日期

在以前的struts2.0版本中s:datetimepicker只需要在head标签处设置<s:head theme"ajax"/>&#xff0c;就可以直接使用s:datetimepicker的标签了。而在2.1.6版本中不能直接这样使用了&#xff0c;将datetimepicker移除了。原因是此标签调用了dojo的datetimepicke…

python数据挖掘 百度云,常用数据挖掘算法总结及Python实现高清完整版PDF_python数据挖掘,python数据分析常用算法...

常用数据挖掘算法总结及Python实现 高清完整版PDF第一部分数据挖掘与机器学习数学基础第一章机器学习的统计基础1.1概率论l概率论基本概念样本空间我们将随机实验E的一切可能基本结果组成的集合称为E的样本空间,记为S。样本空间的元素,即E的每一个可能的结果,称为样本点。样本空…

Dvbbs如何为每个板块设置斑竹

Step 1. 论坛管理 | 管理 |国际结算111 (基本设置) | 论坛版主Step 2. 用户管理 | 点击"搜索" | 所有用户 |zai 用户名栏 点击 "玉树临风"| 修改 email address转载于:https://www.cnblogs.com/cy163/archive/2006/01/29/324218.html

汇编[bx+idata](8086)

[bxidata]表示一个内存单元&#xff0c;它的偏移地址的值就是bxidata 对于指令&#xff1a; mov ax,[bx200]将偏移地址为bx200的内存单元送到ax中&#xff0c;段地址存放在ds中,当cpu执行这条语句时&#xff0c;从ds获取段地址&#xff0c;bx200当做偏移地址&#xff0c;从而获…

1补码 2补码_8085微处理器中8位数字的1和2的补码

1补码 2补码1的8位补码 (1s compliment of 8 bits number) Problem statement: 问题陈述&#xff1a; To perform 1scompliment of 8 bits number using 8085 microprocessor. 使用8085微处理器执行1的8位数字补码。 Algorithm: 算法&#xff1a; Load the accumulator with…

例解基于UML的面向对象分析与设计

http://www.cnblogs.com/leoo2sk/archive/2008/11/08/1329468.html转载于:https://www.cnblogs.com/zhangzt/archive/2011/04/12/2013566.html

php 上传多个txt文件上传,一个多文件上传的例子(原创)

一个多文件上传的例子(原创)更新时间&#xff1a;2006年10月09日 00:00:00 作者&#xff1a;//filename:multi_upload.phpif($ifupload){$pathAddSlashes(dirname($PATH_TRANSLATED))."\\upload\\";for($i1;$i<8;$i){$files"afile".$i;if(${$files}!&…

“软件工业奥斯卡”SYS-CON 读者选择奖: .NET 开发

SYS-CON 媒体&#xff08;www.sys-con.com)在近日揭晓了一年一度的" 读者选择奖" .这个奖项有“软件工业奥斯卡”之称.今年是第10届&#xff0c;有超过1.7万的SYS-CON读者参与了投票&#xff0c;分别评选出了在SOA、Web Services, Java 和 XML技术等领域的最佳产品、…

DI和SI

si和di是8086CPU中和bx功能相近的寄存器&#xff0c;di和si不能分成两个8位寄存器来使用。下面的3组指令实现了相同的功能&#xff1a; mov bx,0 mov ax,[bx]mov si,0 mov ax,[si]mov di,0 mov ax,[di]我们遇到si和di时&#xff0c;就往bx上靠&#xff0c;基本上bx什么功能di和…

mysql查找最大值最小值_查找两个8位数字的最大值| 8086微处理器

mysql查找最大值最小值Problem statement: 问题陈述&#xff1a; To find maximum of two 8-bit numbers using 8086 Microprocessor. 使用8086微处理器查找最多两个8位数字。 Algorithm: 算法&#xff1a; Move the first number to register AL. 移动第一个数字以注册AL。…

无线智能路由器家长控制宽带

家长对控制孩子的上网问题颇为头痛&#xff0c;其实只要方法用对&#xff0c;控制孩子上网是完全没有问题的。我总结了三条供家长们分享。 6-16岁的孩子正是学习知识&#xff0c;塑造性格最佳时期&#xff0c;辨别事物也最为薄弱。要想控制孩子上网&#xff0c;首先要在思想上引…

php中in array循环,在php中in_array的使用方法

在php中in_array的使用方法发布时间&#xff1a;2020-09-29 17:00:16来源&#xff1a;亿速云阅读&#xff1a;69作者&#xff1a;小新在php中in_array的使用方法&#xff1f;这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来…