本文主要是介绍uva 10491 Cows and Cars,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
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
大意:
你参加一个电视节目,主持人给你n+m个门,其中有n个门后面是牛,m个门后面是车,你你现在选一个门,然后主持人告诉show个门后面是牛,然后你再选除了当前这个们之外的剩下的门,问你选到车的概率是多少。
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<fstream>
#include <climits>
using namespace std;
//fstream input,output;int main()
{ios::sync_with_stdio(false);double cow,car,show;while(cin>>cow>>car>>show){cout<<setprecision(5)<<fixed<<cow/(cow+car)*car/(car+cow-show-1)+car/(cow+car)*(car-1)/(cow+car-show-1)<<endl;}
// input.close();
// output.close();return 0;
}
解答:
这题刚开始看错了,以为你每选一次,主持人只会告诉你其中一个门后面有牛。鳖了一天也没做对,最后看看别人的题解,结果发现居然如此简单!! 作为一个编程题,你连循环都没用,真是可以了!= =
其实就是一个全概率公式而已,由于你是选两次,所以分成上来先选到牛的概率,和先选到车的概率。
如果上来先选到牛,同时主持人再告诉你show个门后面有牛那么就用刚开始选牛的概率乘以门数减去主持人的提示数减去你当前的选的这个牛后选到车的概率,同理上来选车的概率乘以门数减去提示数减去你当前选中的门的概率,两个加一起即可。
这篇关于uva 10491 Cows and Cars的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!