UVA 10491 Cows and Cars

题目描述

Description

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 sucha 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 NCOW S 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

题目链接

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1432

解题思路

UVA的题目永远这么难读!

题意:有牛和车躲在门内,你做一个选择,但是还不能看答案,然后知情人会帮助你,你会打开几个门里面全是牛,但是注意一点,知情人知道你选的那个门里面是牛还是车,但即便他知道是牛他也不能告诉你。知情人打开门后,你必定重新选择另一个门(要是可能保持不变的话又不同了),问你这次选到车的概率多少。

知情人会一次性打开door个门(如果你选的就是牛,虽然知情人能打开door个门,但是它不能打开你那个,而题目有一个条件0 <= door < cow,这说明即便你选了一个牛,知情人还是能打开door个门的,是另外的door个门)。

  • 第一次选择牛,概率为cow / (cow + car),然后知情人打开了door个门(自己选的那个不被打开),然后现在实际上只剩下cow + car - door个门(包括自己选的那个)。改变选择,那么现在能选的门的个数为cow + car - door - 1,选到车的概率为car / (cow + car - door - 1)。由于是条件概率所以要相乘:p1 = car / (cow + car) * car / (cow + car - door - 1)
  • 如果第一次选到车,概率为car / (cow + car),然后知情人打开door个门,实际上剩下的门也是cow + car - door。改变选择,那么能选的个数为cow + car - door - 1,车的个数只剩下car - 1(你放弃了之前选择的那个),选到车的概率就是(car - 1) / (cow + car - door - 1),两者相乘:p2 = car / (cow + car) * (car - 1) / (cow + car - door - 1)

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>

int main()
{
double cow, car, door;
while (~scanf("%lf%lf%lf", &cow, &car, &door))
{
double p1 = 1.0 * cow / (cow + car) * car / (cow + car - door - 1);
double p2 = 1.0 * car / (cow + car) * (car - 1) / (cow + car - door - 1);
printf("%.5f\n", p1 + p2);
}
return 0;
}