hduoj1099!【数学】

/*Lottery 
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2451    Accepted Submission(s): 1111

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
 
Author
eddy
*/
#include<stdio.h>
int gcd(__int64 y, __int64 x)
{
	if(x == 0)
	return y;
	else
	return gcd(x,y%x);
}
int main()
{
	__int64 i, j, k,l, inte[23]= {0,1}, num[23], den[23];
	num[1] = 0;
	for(i = 2;i <= 22; i++)
	{	
		num[i] = 1;
		den[i]  = 1;
	}
	for(i = 2; i <= 22; i++)
	{
		for(j = 2; j  <= i; j++)
		{
			
			num[i] = num[i]*j + den[i];	
			den[i] *= j;
			k = gcd(den[i], num[i]);
			den[i] /= k;
			num[i] /= k;
		}
		num[i] *= i;
		inte[i] = num[i] / den[i];
		if(num[i] % den[i] == 0)
		{
			num[i] = 0;
			continue;
		}
		num[i] %= den[i];
		k = gcd(den[i], num[i]);
		num[i] /= k;
		den[i] /= k;
	}
	while(scanf("%d", &i) != EOF)
	{
		if(num[i] == 0)
		printf("%I64d\n", inte[i]);
		else
		{
			if(inte[i] > 10)
			printf("   ");
			else
			printf("  ");
			printf("%I64d\n%I64d ", num[i],inte[i]);
			l = den[i];
			while(l)
			{
				l/=10;
				printf("-");
			}
			printf("\n");
			if(inte[i] > 10)
			printf("   ");
			else
			printf("  ");
			printf("%I64d\n",den[i]);
		}
	}
	return 0;
}
				


题意有点难理解:一套彩票有n张,编号由1到n,凑齐这n张能得奖,问平均每买几张彩票会得到完整的一套彩票。(不是直译,这只是我理解的题目的大意,注意“平均每买几张……”),这大概是一个数学期望的问题吧。以n=3为例,要凑齐1、2和3,买一张彩票无论是1、2还是3都是所需的,买中所需彩票的概率是1,期望的买的张数也是1;再买一张,现在需要的是2或3,一次买中所需的彩票的概率为2/3,忽略张数是int,假设平均每买x张(不是每买x张就一定有,而是平均)就能得到所需的,有(2/3)*x=1,于是x=1/(2/3);假设得到2,现在需要3,买一张是3的概率是1/3,显然平均每买1/(1/3)=3张就有一张3.所以sum=1+1/(2/3)+1/(1/3)。likewise,sum(n)=(1+1/(n-1/n)+1/(n-2/n)+...+1/(1/n))=n*(1/n+1/(n-1)+1/(n-2)+...+1),输出的就是sum,并且题目要求以真分数的形式输出。

还有个关键在于输出格式,需要稍作处理。

打表法是最省时间的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值