zoukankan      html  css  js  c++  java
  • 第六周 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 }
  • 相关阅读:
    springcloud学习(五)之GateWay
    springcloud学习(四)之Feign
    springcloud学习(三)之Hystrix
    springcloud学习(二)之Ribbon
    springcloud学习(一)之Eureka
    关于ES6尾调用优化
    CSS常考知识点
    Chrome动画调试最佳实践
    HTML常考知识点
    代码之旅:基础规范
  • 原文地址:https://www.cnblogs.com/huangguodong/p/4745176.html
Copyright © 2011-2022 走看看