HDOJ 1099 Lottery

来源:互联网 发布:易语言创建sql数据库 编辑:程序博客网 时间:2024/05/22 02:06

我不会算法哎,所以在杭电的首题就想找个算法来着
可是看了一道极短的题,忍不住去做了
单击666传送
题面如下:
Lottery

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张彩票,都要设立奖项,有一句话说是平均做一套完整的券需要多少张优惠券。乍一看,好像是不会做的,其实就是数学期望。求s=n*(1+1/2+1/3+…+1/n) 然后这个输出格式还是强的,整数直接输出,但是很多情况不是啊,很好,横空出世一个代分数,这个真的蒙逼,看了好久都没有懂,所以这个输出控制真是666,其他的你通分一下算一下呗,还是比较简单的,但是int一定会是超的,所以直接LL吧,随便搞一下就好了

#include <stdio.h>typedef long long LL;LL a[25];LL gcd(LL a,LL b){    while(b){        int t=a%b;        a=b;        b=t;    }    return a;}int main(){a[1]=1;for(LL i=2;i<23;i++)a[i]=i/gcd(i,a[i-1])*a[i-1];LL n;while(~scanf("%lld",&n)){LL s=0,f=a[n];for(LL i=1;i<=n;i++)    s+=f/i;s*=n;LL b=gcd(s,f);s/=b;f/=b;if(s%f==0)printf("%lld\n",s/f);else{            if(s/f> 9)             putchar(' ');            printf("  %d\n",s%f);                printf("%d ",s/f);            LL t = f;              while(t)              {                  putchar('-');                  t/=10;              }              putchar('\n');              if(s/f>9) putchar(' ');              printf("  %d\n",f);}   }    return 0;}
0 0