LightOJ 1248

题意:http://www.lightoj.com/volume_showproblem.php?problem=1248

  投掷出第一个未出现的点数的概率为n/n = 1, 因为第一次投掷必然是未出现的。

  第二个未出现的点数第一次出现的概率为 (n - 1) / n,因为有一个已经投掷出现过。

  第i个未出现的点数第一次出现的概率为 (n - i) / i, 这满足几何分布。

  其期望E = 1/p

  所以期望为n *(1 + 1 / 2 + 1 / 3 + ... 1 / n)。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
#include<string>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define N 100006
#define Lson rood<<1
#define Rson rood<<1|1
double q[N];
void Init()
{
    q[0]=0;
    for(int i=1;i<N;i++)
        q[i]=q[i-1]+1.0/i;
}
int main()
{
    int T,n,t=1;
    Init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        printf("Case %d: %.6f
",t++,1.0*q[n]*n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/a719525932/p/7804288.html