uva 10491 Cows and Cars

2024-03-24 08:58
文章标签 uva cows cars 10491

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/841090

相关文章

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^