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
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; }
【扩展阅读】