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 }
  • 相关阅读:
    JAVA类与对象(十)-----抽象类
    JAVA类与对象(九)------多态
    JAVA类与对象(八)-----重写
    JAVA类与对象(七)------继承
    Mysql与Oracle区别
    redis缓存技术学习
    关于java中B/S架构
    关于java中C/S架构,创建服务器和客户端
    JQuery 轮播图片
    Kendo中ListView 无分页控件显示和有分页控件显示
  • 原文地址:https://www.cnblogs.com/huangguodong/p/4745176.html
Copyright © 2011-2022 走看看