• 第六周 E题 期望.....


    Description

    Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that means when you throw the dice, the probability of occurring any face is equal.

    For example, for a fair two sided coin, the result is 3. Because when you first throw the coin, you will definitely see a new face. If you throw the coin again, the chance of getting the opposite side is 0.5, and the chance of getting the same side is 0.5. So, the result is

    1 + (1 + 0.5 * (1 + 0.5 * ...))

    = 2 + 0.5 + 0.52 + 0.53 + ...

    = 2 + 1 = 3

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 105).

    Output

    For each case, print the case number and the expected number of times you have to throw the dice to see all its faces at least once. Errors less than10-6 will be ignored.

    Sample Input

    5

    1

    2

    3

    6

    100

    Sample Output

    Case 1: 1

    Case 2: 3

    Case 3: 5.5

    Case 4: 14.7

    Case 5: 518.7377517640

    题意:给你一个n面的骰子,要你求,这个骰子每面出现一次的期望....

    题解:第i面的骰子的期望就是n/i

    代码如下:

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int T,N=0;
     5     scanf("%d",&T);
     6     while(T--)
     7     {
     8         N++;
     9         double n;
    10         scanf("%lf",&n);
    11         double ans=0;
    12         for(double i=1.0;i<=n;i++)
    13             ans+=n/i;
    14         printf("Case %d: %.10lf
    ",N,ans);
    15     }
    16     return 0;
    17 }
  • 相关阅读:
    赫夫曼树编码
    根据先序和中序实现后序
    C++语言实现开心消消乐
    C语言风格实现的开心消消乐
    动态规划
    leetcode dp wordbreakII
    欧拉回路
    欧拉通路是否存在
    Python|多任务:线程、进程、协程--你想要的都在这里
    网络通信:socket、udp与tcp
  • 原文地址:https://www.cnblogs.com/huangguodong/p/4745176.html
Copyright © 2020-2023  润新知