zoukankan      html  css  js  c++  java
  • SPOJ Favorite Dice(数学期望)

    BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:

    What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?

    Input

    The first line of the input contains an integer t, the number of test cases. t test cases follow.

    Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD's die.

    Output

    For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.

    Example

    Input:
    2
    1
    12
    
    Output:
    1.00
    37.24

     

    题意:

    甩一个n面的骰子,问每一面都被甩到的次数期望是多少。

    思路:

    比较简单,公式:初始化dp[]=0;  dp[i]=i/n*dp[i]+(n-i)/n*dp[i+1]+1;  化简逆推即可。  求的是dp[0];

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    #include<memory>
    using namespace std;
    double dp[2000];
    int main()
    {
        int T,i,j,n;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n); dp[n]=0;
            for(i=n-1;i>=0;i--) dp[i]=(dp[i+1]*(n-i)/n+1)*n/(n-i);
            printf("%.2lf
    ",dp[0]);
        } return 0;
    }
  • 相关阅读:
    .net core 经典面试题
    面试常问概念类问题
    常见 .net 面试题目
    Linux 最常用150个命令汇总
    .net core 国际化(web通用版)
    vim 命令合集
    解决Mariadb安装时的Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-qenllaxj/mysqlclient/报错
    正则表达式
    python中的JWT
    chapter2.3、react高阶组件,装饰器
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8042721.html
Copyright © 2011-2022 走看看