UVa 10491 Cows and Cars (概率&广义三门问题 )
10491 - Cows and Cars
Time limit: 3.000 seconds
In television contests, participants are often asked to choose one from a set of or doors for example, one or several of which lead to different prizes. In this problem we will deal with a specific kind of such a contest. Suppose you are given the following challenge by the contest presenter:
In front of you there are three doors. Two of them hide a cow, the other one hides your prize - a car.
After you choose a door, but before you open it, I will give you an hint, by opening one of the doors which hides a cow (I'll never open the door you have chosen, even if it hides a cow). You will then be able to choose if you want to keep your choice, or if you wish to change to the other unopened door. You will win whatever is behind the door you open.
In this example, the probability you have of winning the car is 2/3 (as hard as it is to believe), assuming you always switch your choice when the presenter gives you the opportunity to do so (after he shows you a door with a cow). The reason of this number (2/3) is this - if you had chosen one of the two cows, you would surely switch to the car, since the presenter had shown you the other cow. If you had chosen the car, you would switch to the remaining cow, therefore losing the prize. Thus, in two out of three cases you would switch to the car. The probability to win if you had chosen to stick with your initial choice would obviously be only 1/3, but that isn't important for this problem.
In this problem, you are to calculate the probability you have of winning the car, for a generalization of the problem above:
- The number of cows is variable
- The number of cars is variable (number of cows + number of cars = total number of doors)
- The number of doors hiding cows that the presenter opens for you is variable (several doors may still be open when you are given the opportunity to change your choice)
You should assume that you always decide to switch your choice to any other of the unopen doors after the presenter shows you some doors with cows behind it.
Input
There are several test cases for your program to process. Each test case consists of three integers on a line, separated by whitespace. Each line has the following format:
NCOWS NCARS NSHOW
Where NCOWS is the number of doors with cows, NCARS is the number of doors with cars and NSHOW is the number of doors the presenter opens for you before you choose to switch to another unopen door.
The limits for your program are:
1 <= NCOWS <= 10000
1 <= NCARS <= 10000
0 <= NSHOW < NCOWS
Output
For each of the test cases, you are to output a line containing just one value - the probability of winning the car assuming you switch to another unopen door, displayed to 5 decimal places.
Sample input
2 1 1
5 3 2
2000 2700 900
Sample output
0.66667
0.52500
0.71056
【历史介绍】
三门问题(Monty Hall problem)亦称为蒙提霍尔问题、蒙特霍问题或蒙提霍尔悖论,大致出自美国的电视游戏节目Let's Make a Deal。问题名字来自该节目的主持人蒙提·霍尔(Monty Hall)。参赛者会看见三扇关闭了的门,其中一扇的后面有一辆汽车,选中后面有车的那扇门可赢得该汽车,另外两扇门后面则各藏有一只山羊。当参赛者选定了一扇门,但未去开启它的时候,节目主持人开启剩下两扇门的其中一扇,露出其中一只山羊。主持人其后会问参赛者要不要换另一扇仍然关上的门。问题是:换另一扇门会否增加参赛者赢得汽车的概率?
【题意】
现在题目变成了:给你NCOWS只牛,NCARS辆车,NSHOW扇开启的门( NSHOW < NCOWS)
问:换门后赢得车的概率是?
【思路】
我们首先用分类讨论的思想得出三门问题的答案:
P(赢得汽车)
=P(最开始选的那扇门后是山羊)*P(在最开始选的那扇门后是山羊的情况下剩下那扇门后是车)+P(最开始选的那扇门后是车)*P(在最开始选的那扇门后是车的情况下剩下那扇门后是车)
=2/3*1+1/3*0=2/3
用专业术语来说,设A={最开始选的那扇门后是山羊},B={第二次选的门后是车}
则由全概率公式得:
那么对于此题,同样可以用上面的公式计算得出:
【完整代码】
/*0.018s*/ #include<stdio.h> int main(void)
{
int cow, car, show, temp;
while (~scanf("%d%d%d", &cow, &car, &show))
{
temp = cow + car - 1;
printf("%.5f\n", (double)temp * car / ((temp + 1) * (temp - show)));
}
return 0;
}
【扩展阅读】
UVa 10491 Cows and Cars (概率&广义三门问题 )的更多相关文章
- UVa 10491 - Cows and Cars(全概率)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 10491 - Cows and Cars
題目:有m+n個們,每個門後面有牛或者車:有n仅仅牛,m輛車,你選擇当中1個: 然後打開当中的k你沒有選中的門後是牛的,問你改變選時得到車的概率. 說明:數學題,概率.全概率公式就可以: 說明:第10 ...
- UVA 10491 Cows and Cars (全概率公式)
#include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...
- 10491 - Cows and Cars
描述:要么全选择牛,要么选择一辆车和p-1头牛,那么剩下n+m-p道门可以选择,求选择p道门以后要选择到车的概率 #include <cstdio> int main() { //freo ...
- UVA10491 - Cows and Cars(概率)
UVA10491 - Cows and Cars(概率) 题目链接 题目大意:给你n个门后面藏着牛.m个门后面藏着车,然后再给你k个提示.在你作出选择后告诉你有多少个门后面是有牛的,如今问你作出决定后 ...
- Cows and Cars UVA - 10491 (古典概率)
按照题目的去推就好了 两种情况 1.第一次选择奶牛的门 概率是 a/(a+b) 打开c扇门后 除去选择的门 还剩 a-1-c+b扇门 则选到车的概率为b/(a-1-c+b) 2.第一次选择车的门 ...
- 紫书 例题 10-10 UVa 10491(概率计算)
公式很好推,表示被高中生物遗传概率计算虐过的人 这个公式简直不需要动脑 #include<cstdio> using namespace std; int main() { double ...
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...
- UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)
UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...
随机推荐
- QT里嵌入Python
刚看到一个软件,叫做,明明是QT做的,却带了很多pyd文件(Python编译后的文件),上网一查,果然有这套相关的东西: https://doc.qt.io/archives/qq/qq23-pyth ...
- Android开发之发送邮件功能的实现(源代码分享)
Android开发中可能会碰到如何发送邮件的困扰,之前我也查了相关的文档,博友们也分享了不少的发送邮件的办法,总共有3种把,我仔细阅读了下,发现有的讲的太过复杂跟麻烦,不够清晰,我今天就来分享下我认为 ...
- CC++初学者编程教程(11) 配置Windows数据库服务器
1.我们新建一个虚拟机. 2. 选择默认的WorkStation10.0. 3.我们选择VS2012的镜像. 4.我们设置用户密码,跳过WindowsSever2012密钥 5.我们选择是,稍后手动激 ...
- Linux进程间通信——使用消息队列
下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管道 一.什么是消息队列 消息队列提 ...
- Hoeffding连接到机器学习
统计学场景: 一个罐子中有红球和绿球,红球比例$v$未知,数量未知,如何得到红球比例?方法---随机抽样N个球,在其中红球占比为$u$ 由hoeffding可以知道:$P(|u-v|>\epsi ...
- 微信公众帐号开发。大家是用框架还是自己写的流程。现在遇到若干问题。请教各路大仙 - V2EX
微信公众帐号开发.大家是用框架还是自己写的流程.现在遇到若干问题.请教各路大仙 - V2EX 微信公众帐号开发.大家是用框架还是自己写的流程.现在遇到若干问题.请教各路大仙
- AndroidUI 视图动画-旋转动画效果 (RotateAnimation)
RotateAnimation,能实现Android的视图的旋转效果,废话不多说直接上代码. 新建一个Android 项目,在activity_main.xml中添加一个按钮,然后使用Relative ...
- OC中ARC forbids explicit message send of release错误(转)
ARC forbids explicit message send of'release' 很显然,是ARC的问题. 错误原因:在创建工程的时候点选了“Use Automatic Reference ...
- 使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表)
前言 打造一款完整可用的Android播放器有许多功能和细节需要完成,也涉及到各种丰富的知识和内容,本章将结合Fragment.ViewPager来搭建播放器的主界面,并实现本地播放基本功能.系列文章 ...
- 斯坦福IOS开发第五课(第一部分)
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/27706991 作者:小马 因为第五课的内容比較多.分两部分来写. 一 屏幕旋转基本 ...