跳到正文

题解

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

_jry_qwq_约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月26日
收录
2026年7月29日
更新
2026年11月12日
篇幅
约 2 分钟
获赞
1
题目
P17151

版本

  1. v1当前

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

思路

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

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

nn+m\frac{n}{n+m}

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

code
C++
#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;
}
原文
存档评论6 条抓取于