Lottery
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2420 Accepted Submission(s): 1097
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/n+n/(n-1)+n/(n-2)+……n/2+n/1
AC代码:#include <stdio.h>
#define ll __int64
//author:XXYY
ll gcd(ll a,ll b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main(){
ll n,i,m,z,len1,len2,x,n1,n2,y;
while(~scanf("%I64d",&n)){
m=0;
z=1;
for(i=n;i>0;i--){//通分
m=n*z+m*i;
z*=i;
x=gcd(m,z);
m/=x;
z/=x;
}
if(m%z){
y=m/z;
m=m%z;
n1=y;
len1=len2=0;
while(n1){
len1++;
n1/=10;
}
n2=z;
while(n2){
len2++;
n2/=10;
}
for(i=0;i<=len1;i++)
printf(" ");
printf("%I64d\n",m);
printf("%I64d ",y);
for(i=0;i<len2;i++)
printf("-");
printf("\n");
for(i=0;i<=len1;i++)
printf(" ");
printf("%I64d\n",z);
}
else
printf("%I64d\n",m/z);
}
return 0;
}
看到这么小的范围,暴力打表也是可以的:
#include<stdio.h>
int main(){
int n;
while(~scanf("%d",&n)){
switch(n){
case 1:
printf("1\n");
break;
case 2:
printf("3\n");
break;
case 3:
printf(" 1\n5 -\n 2\n");
break;
case 4:
printf(" 1\n8 -\n 3\n");
break;
case 5:
printf(" 5\n11 --\n 12\n");
break;
case 6:
printf(" 7\n14 --\n 10\n");
break;
case 7:
printf(" 3\n18 --\n 20\n");
break;
case 8:
printf(" 26\n21 --\n 35\n");
break;
case 9:
printf(" 129\n25 ---\n 280\n");
break;
case 10:
printf(" 73\n29 ---\n 252\n");
break;
case 11:
printf(" 551\n33 ----\n 2520\n");
break;
case 12:
printf(" 551\n37 ----\n 2310\n");
break;
case 13:
printf(" 9473\n41 -----\n 27720\n");
break;
case 14:
printf(" 13433\n45 -----\n 25740\n");
break;
case 15:
printf(" 18581\n49 -----\n 24024\n");
break;
case 16:
printf(" 4129\n54 -----\n 45045\n");
break;
case 17:
printf(" 340463\n58 ------\n 720720\n");
break;
case 18:
printf(" 620743\n62 ------\n 680680\n");
break;
case 19:
printf(" 1662439\n67 -------\n 4084080\n");
break;
case 20:
printf(" 3704479\n71 -------\n 3879876\n");
break;
case 21:
printf(" 408335\n76 ------\n 739024\n");
break;
case 22:
printf(" 46533\n81 ------\n 235144\n");
break;
}
}
return 0;
}