UVa11427 Expect the Expected

第一次做这种题,顺便花了一个上午学习了一下概率与期望的相关知识.

本题需要的知识点:

1. 条件概率与独立性.

2. 期望的几何分布, 通俗的说---,

做一件事(每次独立)成功概率是P,第一次做成时做的次数的期望就是1/P

学了这些, 你就能 做出来 看得懂题解了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e2 + 20;

int N;
double P;double d[MAXN][MAXN];

int main()
{
    int T, t = 0; cin>>T;
    while(T--)
    {
        int x, y;
        scanf("%d/%d%d", &x, &y, &N);
        P = 1.0 * x / y;
        
        memset(d, 0, sizeof(d));
        d[0][0] = 1.0, d[0][1] = 0.0;
        for(int i = 1; i <= N; i++)
            for(int j = 0; j * y <= x * i; j++){
                d[i][j] = d[i - 1][j] * (1 - P);
                if(j) d[i][j] += d[i - 1][j - 1] * P;
             }
         double Q = 0.0;
         for(int j = 0; j * y <= x * N; j++) Q += d[N][j];
         printf("Case #%d: %d
", ++t, (int) (1 / Q));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wsmrxc/p/9289384.html