UVA 题目10288 Coupons(期望)

探讨了在每个盒子里放置一个编号从1到n的优惠券,且每种编号优惠券各需一个来获得奖品的情况下,平均需要购买多少个盒子才能集齐所有优惠券的问题。提供了一个AC代码实现,通过数学期望的方法解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个球放在n个盒子里,每次取每个盒子的概率一样,问取出n个球的期望是多少

ac代码

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#define LL long long
using namespace std;
LL gcd(LL a,LL b)
{
    if(a<b)
    {
        swap(a,b);
    }
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int fun(LL x)
{
    int ans=0;
    while(x)
    {
        x/=10;
        ans++;
    }
    return ans;
}
int main()
{
    LL n;
    while(scanf("%lld",&n)!=EOF)
    {
        LL i;
        LL fz=n,fm=n;
        for(i=1;i<n;i++)
        {
            fz=fz*(n-i)+n*fm;
            fm=fm*(n-i);
            LL Gcd=gcd(fm,fz);
            fm/=Gcd;
            fz/=Gcd;
        }
        if(fm==1)
        {
            printf("%lld\n",fz);
        }
        else
        {
            LL fi;
            fi=fz/fm;
            fz%=fm;
            int c1=fun(fi);
            int c2=max(fun(fz),fun(fm));
            for(i=0;i<c1;i++)
                printf(" ");
            printf(" %lld\n",fz);
            printf("%lld ",fi);
            for(i=0;i<c2;i++)
                printf("-");
            printf("\n");
            for(i=0;i<c1;i++)
                printf(" ");
            printf(" %lld\n",fm);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值