When I was young , my father is a senior gaming enthusiast . One day , we saw a old man in the street . He had a dice and played with other people .
Every turn the gambler gives k RMB to the old man and throw the dice . If the point is 1, 2 or 3,he will win 2k RMB back ,otherwise he will get nothing .
My father told me , “I can win all his money by the following strategy”.
“Each turn , I bet on 1 RMB first . If I lose , I will bet on 2 RMB . If I still lose ,I will bet on 4,8,16,…, and so on , until I win . And start to bet on 1 RMB , do the same thing again .”
“If I don’t have enough money to bet , I will bet on all my money.”
Now the question is , if the dice is even , my father has n RMB , the old man has m RMB , they stop until one of them lose all his money , what’s the probability of my father’s victory .
Input
The input contains multiple test cases.(No more than 20)
In each test case:
The only line contains two numbers n, m. (0≤n,m≤2000000), indicate my father’s money and the old man’s . We guarantee max(n,m)≥1.
Output
For each test case, print the answer in five decimal .
样例输入
1 0
3 3
样例输出
1.00000
0.50000
这题的题意大概就是:因为筛子1,2,3能赢,所以概率是0.5,固定不变。每次从一块钱开始,赢了拿两倍的钱,输了就没了本钱,如果第一次输了,第二次就放2元(每次*2),但凡赢一次,就能把之前输的一起拿回来(优先保证不亏),如果第一次1元就赢了,那就稳赚不赔。以这种方式赚钱速度慢但是稳。
解题思路: n/(n+m)便是获胜概率。(举例:假设我方99元,老头1元。每次都是0.5的概率拿他一块钱,而他基本赚不到,因为在我用完99元前我都是0.5的概率赢回本金,赢回本金后再次以1元开始,只要不脸黑到钱用完也就是0.5x0.5x0.5…剩下都是我赢。)
特别注意: 输入是while(~scanf("%lf %lf",&n,&m))意思是输入为空就退出,我之前用了while(scanf("%lf %lf",&n,&m)!=NULL)超时,while(scanf("%lf %lf",&n,&m)!=EOF)错误。
#include<iostream>
using namespace std;
int main()
{
double n,m;
while(~scanf("%lf %lf",&n,&m))
{
printf("%.5lf\n",n/(n+m));
}
return 0;
}