YYS FZU - 2278 (期望)JAVA

题目链接:

G - YYS

 FZU - 2278 

题目大意:

我们现在想要收集到n个卡片,现在已知抽到每种卡片的概率为1/n,现在每隔(n-1)!天就可以进行一次抽奖,问收集齐所有卡片的期望天数。

具体思路:

第一天获得一张新的卡片的概率是n/n,第二天获得新的卡片的概率是(n-1)/n。然后来分析一波第二天的概率,(n-1)/n代表的是当从n-1张抽出除了第一张以外的,都是满足条件的。打个比方,如果是抽中的概率是3/5,那么也就是说 抽(5/3)张一定能中。那么第二天的期望类比,也就是第二天的期望是(n/(n-1)).然后把每一天的都加起来就行了。

import java.util.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int T=cin.nextInt();
        for(int q=1; q<=T; q++)
        {
            int n;
            n=cin.nextInt();
            BigInteger tmp= new BigInteger("1");
            for(int i=1; i<=n; i++)
            {
                Integer t1=new Integer(i);
                BigInteger t2= new BigInteger(t1.toString());
                tmp=tmp.multiply(t2);
            }
            BigInteger ans= new BigInteger("0");
            for(int i=1; i<=n; i++)
            {
                Integer t1=new Integer(i);
                BigInteger t2=new BigInteger(t1.toString());
                ans=ans.add(tmp.divide(t2));
            }
            System.out.print(ans);
            System.out.println(".0");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/10728249.html