Lottery
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 104 Accepted Submission(s): 63
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
Sample Output
3
5
11 --
12
340463
58 ------
720720
#include<iostream>
using namespace std;
int getBit(int n)
{
int i=0;
while(n!=0){
n/=10;
++i;
}
return i;
}
void print(int s,int n,int d){
int i,len=getBit(s)+1;
for(i=0;i<len;++i)
putchar(' ');
printf("%d\n%d ",n,s);
len=getBit(d);
for(i=0;i<len;++i)
putchar('-');
putchar('\n');
len=getBit(s)+1;
for(i=0;i<len;++i)
putchar(' ');
printf("%d\n",d);
return;
}
int gcd(int n,int d){
while( n!=d ) {
if(n>d) n=n-d;
else d=d-n;
}
return n;
}
int main()
{
int n,i;
int sum,g;
int numerator;
int denominator;
while(cin>>n){
sum=0;
numerator=0;
denominator=1;
for(i=1;i<=n;i++){
numerator=numerator*i+n*denominator;
denominator*=i;
g=gcd(numerator,denominator);
numerator/=g;
denominator/=g;
sum+=numerator/denominator;
numerator%=denominator;
}
if(numerator==0)
printf("%d\n",sum);
else
print(sum,numerator,denominator);
}
return 0;
}
此题还算比较简单(只要看懂题目.....一个开始楞是看不懂.......汗),比较考验输出。