10288 - Coupons(赠券收集问题)(概率)

roblem F

Coupons

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

 

Coupons in cereal boxes are numbered 1 to n, and a set of one of each is required for a prize (a cereal box, of course). With one coupon per box, how many boxes 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<=33, giving the size of the set of coupons. Input is terminated by end of file.

Output

For each input line, output the average number of boxes 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 output.

Sample Input

2
5
17

Sample Output

3 
   5
11 --
   12
   340463
58 ------
   720720


当有n种图案需要收集时,平均需要的次数就是这个——期望E(T);


本题还有个考查点:分数的表示:

看上一篇分数运算

代码:

#include <cassert>  
#include <cstdio>  
#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <cstring>  
#include <stdlib.h>  
  
using namespace std;  
typedef long long ll;  
  
ll gcd(ll a,ll b){  
    return b?gcd(b,a%b):a;  
}  
struct frac{  
    ll up,low;  
    frac(ll up=0,ll low=1){  
        if(low<0) up=-up,low=-low;  
        assert(low);  
        ll g=gcd(abs(up),low);  
        this->up=up/g,this->low=low/g;  
    }  
    frac operator + (const frac &b) const  
    {  
        return frac(up * b.low + low * b.up, low * b.low);  
    }  
    frac operator - (const frac &b) const  
    {  
        return frac(up * b.low - low * b.up, low * b.low);  
    }  
    frac operator * (const frac &b) const  
    {  
        return frac(up * b.up, low * b.low);  
    }  
    frac operator / (const frac &b) const  
    {  
        return frac(up * b.low, low * b.up);  
    }  
    bool operator < (const frac &b) const  
    {  
        return up * b.low < low * b.up;  
    }  
    bool operator == (const frac &b) const  
    {  
        return up * b.low == low * b.up;  
    }  
    bool operator > (const frac& b) const  
    {  
        return b < *this;  
    }  
    bool operator <= (const frac& b) const  
    {  
        return !(b < *this);  
    }  
    bool operator >= (const frac &b) const  
    {  
        return !(*this < b);  
    }  
    bool operator != (const frac &b) const  
    {  
        return up * b.low != low * b.up;  
    }  
    frac operator += (const frac &b)  
    {  
        return *this = *this + b;  
    }  
    frac operator -= (const frac &b)  
    {  
        return*this = *this - b;  
    }  
    frac operator *= (const frac &b)  
    {  
        return *this = *this * b;  
    }  
    frac operator /= (const frac &b)  
    {  
        return *this = *this / b;  
    }  
  
};  
  
int main(){  
    int n;  
    while(~scanf("%d",&n)){  
        frac ans[100];  
        ans[1]=frac(1,1);  
        for(int i=2;i<=n;i++){  
            ans[i]=ans[i-1]+frac(1,i);  
        }  
        ans[n]*=frac(n,1);  
        ll a,b,c;  
        a=ans[n].up/ans[n].low;  
        b=ans[n].up%ans[n].low;  
        c=ans[n].low;  
        if(a&&b){  
            ll t=1;  
            while(a>=t){  
                printf(" ");  
                t*=10;  
            }  
            printf(" ");  
        }  
        if(b) printf("%lld\n",b);  
        if(a) printf("%lld",a);  
        if(b){  
            printf(" ");  
            ll t=1;  
            while(c>=t){  
                printf("-");  
                t*=10;  
            }  
        }  
        printf("\n");  
        if(a&&b){  
            ll t=1;  
            while(a>=t){  
                printf(" ");  
                t*=10;  
            }  
            printf(" ");  
        }  
       if(b)printf("%lld\n",c);  
    }  
    return 0;  
} 

其实知道了公式,最难的还是分数的表示.............................................................



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值