现在的位置: 首页 > 综合 > 正文

HDU/HDOJ 1099 Lottery

2018年01月20日 ⁄ 综合 ⁄ 共 1635字 ⁄ 字号 评论关闭

 

Lottery

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1042    Accepted Submission(s): 519

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
 
计算方法:
n*(1/1+1/2+1/3+...+1/n)
不过输出非常恶心
要各种注意
还好1Y ^_^
 
 
我的代码:
//n*(1/1+1/2+1/3+...+1/n)
#include<stdio.h>
#include<string.h>

typedef __int64 ll;

ll gcd(ll a,ll b)
{
	if(b==0)
		return a;
	else
		return gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
	return a/gcd(a,b)*b;
}

ll numlen(ll n)
{
	ll l=0;
	while(n)
	{
		n=n/10;
		l++;
	}
	return l;
}

int main()
{
	ll n,son,mother,g,d,i;
	while(scanf("%I64d",&n)!=EOF)
	{
		mother=1,son=0;
		for(i=1;i<=n;i++)
			mother=lcm(mother,i);
		for(i=1;i<=n;i++)
			son=son+mother/i;
		son=son*n;
		g=gcd(mother,son);
		son=son/g;
		mother=mother/g;
		d=son/mother;
		son=son%mother;
		if(son==0)
		{
			printf("%d\n",d);
			continue;
		}
		ll l1=numlen(d);
		ll l2=numlen(mother);
		for(i=0;i<=l1;i++)
			printf(" ");
		printf("%I64d\n",son);
		printf("%I64d ",d);
		for(i=1;i<=l2;i++)
			printf("-");
		printf("\n");
		for(i=0;i<=l1;i++)
			printf(" ");
		printf("%I64d\n",mother);
	}
	return 0;
}

 
 

抱歉!评论已关闭.