1 条题解

  • 0
    @ 2026-11-2 12:34:48

    抛硬币概率(Coins) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iomanip>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        if (!(cin >> n)) return 0;
        vector<double> dp(n + 1, 0.0);
        dp[0] = 1.0;
        for (int i = 1; i <= n; ++i) {
            double p;
            cin >> p;
            for (int j = i; j >= 0; --j) {
                dp[j] = dp[j] * (1.0 - p) + (j > 0 ? dp[j - 1] * p : 0.0);
            }
        }
        double ans = 0.0;
        for (int j = (n + 1) / 2; j <= n; ++j) ans += dp[j];
        cout << fixed << setprecision(10) << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8602
    时间
    2000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者