您的位置:首页 > 其它

HDU 1099 Lottery 乐透彩

2015-07-26 20:20 281 查看
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1099

题目:

Lottery

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2926 Accepted Submission(s): 1309

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张,买1次就集齐了。所以买1张。

假如发行2张,第一次买的序号是1,第二次买中剩下那张的概率是1/2,所以要买两张才能买到第二张,所以要买3张才能才能集齐。

假如发行3张,第一次发的序号是1,要买1张,第二次买中剩下的两张之一的概率是2/3,所以要买3/2张,第三次买剩中最后一张的概率是1/3,所以要买3张,所以要买5+1/2张。

假如发行n张,第一次买中没买过的概率是1,第二次是n-1/n,第三次是n-2/n,第n次是1/n,

而对应需要买的张数是第一次买1张,第二次买n/n-1张,第三次买n/n-2,第n次买n张,所以求的是n/n,n/n-1,……1/n的和。

最后注意输出的结果是约分过的带分数,注意横线是减号。

代码:

#include <iostream>
#include"string.h"
#include"cstdio"
#include"stdlib.h"
#include"algorithm"

using namespace std;
typedef long long int lint;
typedef unsigned long long int ulint;

ulint gcs(ulint a,ulint b)
{
ulint x=a;
ulint y=b;
ulint z;
while(y!=0)
{
z=x%y;
x=y;
y=z;
}
return x;
}

int main()
{
int x;
while(scanf("%d",&x)!=EOF)
{
ulint fenmu=1;
ulint fenzi=1;
ulint tm;
ulint tz;
for(int i=2; i<=x; i++)
{
tm=fenmu*i;
tz=fenzi*i+fenmu;
ulint g=gcs(tm,tz);
fenmu=tm/g;
fenzi=tz/g;
}
fenzi=fenzi*x;
ulint g=gcs(fenmu,fenzi);
fenmu=fenmu/g;
fenzi=fenzi/g;
ulint zs=fenzi/fenmu;
ulint fs=fenzi%fenmu;
if(fs==0)
{
printf("%I64u\n",zs);
}
else
{
int l1=0;
int t=zs;
while(t>0)
{
t=t/10;
l1++;
}
int l2=0;
t=fenmu;
while(t>0)
{
t=t/10;
l2++;
}
for(int i=0;i<=l1;i++)
{
printf(" ");
}
printf("%I64u\n",fs);
printf("%I64u ",zs);
for(int i=0;i<l2;i++)
{
printf("-");
}
printf("\n");
for(int i=0;i<=l1;i++)
{
printf(" ");
}
printf("%I64u\n",fenmu);
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: