UVa 10491 cows and Cars (probability calculation)

Source: Internet
Author: User

UVa 10491 Cows and Cars

The effect of this question is this:

There is a a+b door in front of you, where a fan is behind a cow, and the B fan is behind a car. You can choose a door, but not open it. At this time the host (guest) will help you open another C fan behind the cow's door, and then you make a choice, is to continue to choose the door or another open door. What we need to answer is, in the case of always choosing "another one", what is the probability of a car behind the chosen door?

Data range:

1<=a<=10000, 1<=b<=10000, 0<=c<a.

This is a probability of the calculation problem, generally this topic is mainly to push the formula, pushed out after it is very good to do, the following outlines the idea:

1> if the first choice of the door is a cow, the probability is a A/(A+B), then the host to help out the C cow, the rest of a common a+b-c-1 fan door, at this time to re-select the car is the probability of B/(a+b-c-1), then the total probability is a*b/(a+b-c-1) * (A+B).

2> if the first choice of the door is a car, the probability is b/(a+b), then the host to help out the C cow, the rest of a common a+b-c-1 fan door, at this time to re-select the car is the probability is (b-1)/(a+b-c-1), then the total probability is b* (b-1)/(A +B-C-1) * (a+b).

That would always be the sums of the two, namely b* (a+b-1)/(a+b-c-1) * (a+b). The code is as follows:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int main ()
{
	int a,b,c;
	while (~SCANF ("%d%d%d", &a,&b,&c))
	{
		double s;
		if (!b | | a+b==c+1) s=0;//has no car or only one car and is selected at the beginning, the probability of re-selecting is 0 else s= (double) ((double) (b*
		(a+b-1))/(double) ((a+b-c-1 ) * (a+b));
		printf ("%0.5lf\n", s);
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.