UVa10288 - Coupons 【数学】

1 篇文章 0 订阅
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

假设当前已经拿到了K种奖券
从箱子中取出一种新奖券的概率是(n-K)/n
平均要取n/(n-K)次
所以ans= n1i=0n/(ni)

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf=1e9+7;
struct Num{
    ll fz,fm;
    ll gcd(ll a,ll b)const{
        return b?gcd(b,a%b):a;
    }
    Num(){
        fz=0,fm=1;
    }

    Num(ll fz,ll fm){
        this->fz=fz;
        this->fm=fm;
    }

    Num operator+(const Num&x)const{
        Num ans;
        ans.fm=fm*x.fm;
        ans.fz=fz*x.fm+fm*x.fz;
        ll d=gcd(ans.fz,ans.fm);
        ans.fz/=d;
        ans.fm/=d;
        return ans;
    }
};

int len(ll x){
    int len=0;
    while(x){
        x/=10;
        ++len;
    }
    return len;
}

void print(const Num&x){
    if(x.fm==1){
        printf("%lld\n",x.fz);
    }
    else{
        ll ansInt=x.fz/x.fm;
        int ansIntLen=len(ansInt);
        Num ansFloat=x;
        ansFloat.fz-=x.fm*ansInt;
        int fmLen=len(x.fm);
        for(int i=0;i<=ansIntLen;++i){
            putchar(' ');
        }
        printf("%lld\n",ansFloat.fz);
        printf("%lld ",ansInt);
        for(int i=0;i<fmLen;++i){
            putchar('-');
        }
        putchar('\n');
        for(int i=0;i<=ansIntLen;++i){
            putchar(' ');
        }
        printf("\%lld\n",ansFloat.fm);
    }
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int n;
    while(~scanf("%d",&n)){
        Num ans;
        for(int i=0;i<n;++i){
            ans=ans+Num(n,n-i);
        }
        print(ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值