LightOJ 1248 Dice (III) [期望]

点击打开题目


题意: 给你一个n个点的色子, 掷一次色子获得每个点数的概率相同, 求得到所有点数时掷色子次数的期望值;

思路: 假设已经有k个点数已经出现过, 则掷下一次可能得到未出现过的点数, 期望为1, 也可能得到已出现过的点数, 概率为k / n, 若得到已出现的点数, 则继续掷色子, 所以期望

      E = 1 + k / n (1 + k / n (1 + n / k * (...)))

        = 1 + k / n + (k / n) ^ 2 + (k / n) ^ 3 + ...

        = 1 + k / (n - k)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const double eps = 1e-9;
const int inf = 1 << 30;

int main() {
    int t, v = 1, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        double ans = 0;
        for(int k = 0; k < n; k++) {
            ans += 1 + 1.0 * k / (n - k);
        }
        printf("Case %d: %.8f\n", v++, ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值