题解:P17151 [ICPC 2017 Xi'an R] God of Gamblers

· · 题解

P17151 [ICPC 2017 Xi'an R] God of Gamblers 题解

思路

公平赌博下最终的获胜概率只和初始的资金比例有关

父亲有 n 元,老人有 m 元,总资金 n+m。那么父亲最终赢光所有钱的概率就是:

\frac{n}{n+m}

边界情况:如果 n=0,说明父亲已经没钱了,则概率为 0;如果 m=0,说明老人已经没钱了,则概率为 1。 ::::success[code]


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int n, m;
    while(cin >> n >> m){
        if(n == 0){
            cout << fixed << setprecision(5) << 0.0 << endl;
        }

        else if(m == 0){
            cout << fixed << setprecision(5) << 1.0 << endl;
        }

        else{
            cout << fixed << setprecision(5) << (double)n / (n + m) << endl;
        }
    }

    return 0;
}