HDU 1099.Lottery【数学题,求期望】【12月31】

Lottery

Problem Description
Eddy's company publishes a kind of lottery.This set of lottery which are numbered 1 to n, and a set of one of each is required for a prize .With one number per lottery, how many lottery on average are required to make a complete set of n coupons?
 

Input
Input consists of a sequence of lines each containing a single positive integer n, 1<=n<=22, giving the size of the set of coupons.
 

Output
For each input line, output the average number of lottery required to collect the complete set of n coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of ouput.
 

Sample Input
  
  
2 5 17
 

Sample Output
  
  
3 5 11 -- 12 340463 58 ------ 720720
题意读了好久····就是求n*(1  + 1/2 +  1/3 + ...... + 1/n),分析如下:(http://www.cnblogs.com/arno-my-boke/archive/2015/01/21/4238137.html

题目的大概意思是说一套彩票有编号1到n共n种,张数不限,问你平均买多少张能把编号为1到n的n中彩票全买下来,也就是求期望。
举个例子,当n=5时,第一张有用的概率为1,买一张就行了,第二张有用的概率为4/5,所以买5/4张彩票能买上对你有用的,一次类推,sum=1+5/4+5/3+5/2+5/1=11…5/12
AC代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
long long gcd(long long a, long long b)
{
    long long  r;
    while(b > 0)
    {
         r = a % b;
         a = b;
         b = r;
    }
    return a;
}
int main()
{

    int n;
    while(cin >> n)
    {
        unsigned long long y = 1;
        unsigned long long x = 0;
        for(int i = 1;i <= n; ++i)
        {
            x = y+i*x;
            y *= i;
            long long GCD = gcd(x, y);
            x /= GCD;
            y /= GCD;
        }
        long long GCD = gcd(n, y);
        y /= GCD;
        n /= GCD;
        x *= n;
        //格式控制太操蛋!
        if(x%y == 0) cout << x/y << endl;
        else
        {
            if(x/y > 9) cout <<" ";
            cout <<"  "<<  x%y <<endl;
            cout << x/y <<" ";
            long long zz = y;
            while(zz)
            {
                cout <<"-";
                zz /= 10;
            }
            cout << endl;
            if(x/y > 9) cout <<" ";
            cout <<"  "<< y <<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值