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 ------
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
/ / 标程:
#include<iostream>
#include<cstdio>
using namespace std;
long long gcd(long long a, long long b)
{
return a == 0 ? b : gcd(b%a,a);
}
int judge(long long x)
{
int cnt = 0;
while(x)
{
cnt ++;
x /= 10;
}
return cnt;
}
int main()
{
// freopen("a.txt","r",stdin);
int n, i, j;
while(~scanf("%d",&n))
{
long long molecule(1);//分子
long long denominator(1); // 分母;
for(i = 2; i <= n; i ++)
{
molecule = molecule * i + denominator;
denominator *= i;
long long k = gcd(molecule,denominator);
molecule /= k;
denominator /= k;
}
molecule *= n;
if(molecule % denominator == 0)
{
printf("%lld\n",molecule / denominator);
continue;
}
long long temp = molecule / denominator;
long long tmp = (molecule - temp * denominator);
long long k = gcd(tmp,denominator);
tmp /= k, denominator /= k;
int wa = judge(temp), ws = judge(tmp), wm = judge(denominator);
for(i=0;i<=wa;i++) printf(" "); printf("%lld\n",tmp);
printf("%lld",temp); printf(" ");
for(i=0;i<wm;i++) printf("-"); puts("");
for(i=0;i<=wa;i++) printf(" "); printf("%lld\n",denominator);
}
return 0;
}