UVA 10288 Coupons

来源:互联网 发布:在家网络医疗咨询 编辑:程序博客网 时间:2024/05/21 10:27

CouponsCrawling in process...Crawling failedTime Limit:3000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu

SubmitStatus Practice UVA 10288

Description

Download as PDF

Problem F

Coupons

Input: standard input

Output: standard output

Time Limit:2 seconds

Memory Limit:32 MB

 

Coupons in cereal boxes are numbered1 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 integern, 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 ofn 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

(Math Lovers’ Contest, Source: University of Alberta Local Contest)


题意:题目意思一开始没看懂啦,后来得知是求http://zh.wikipedia.org/wiki/%E8%B4%88%E5%88%B8%E6%94%B6%E9%9B%86%E5%95%8F%E9%A1%8C(戳了你就知道啦)。

思路:看上面的链接啦。

#include <cassert>#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <cstring>#include <stdlib.h>using namespace std;typedef long long ll;ll gcd(ll a,ll b){    return b?gcd(b,a%b):a;}struct frac{    ll up,low;    frac(ll up=0,ll low=1){        if(low<0) up=-up,low=-low;        assert(low);        ll g=gcd(abs(up),low);        this->up=up/g,this->low=low/g;    }    frac operator + (const frac &b) const{return frac(up * b.low + low * b.up, low * b.low);}frac operator - (const frac &b) const{return frac(up * b.low - low * b.up, low * b.low);}frac operator * (const frac &b) const{return frac(up * b.up, low * b.low);}frac operator / (const frac &b) const{return frac(up * b.low, low * b.up);}bool operator < (const frac &b) const{return up * b.low < low * b.up;}bool operator == (const frac &b) const{return up * b.low == low * b.up;}bool operator > (const frac& b) const{return b < *this;}bool operator <= (const frac& b) const{return !(b < *this);}bool operator >= (const frac &b) const{return !(*this < b);}bool operator != (const frac &b) const{return up * b.low != low * b.up;}frac operator += (const frac &b){return *this = *this + b;}frac operator -= (const frac &b){return*this = *this - b;}frac operator *= (const frac &b){return *this = *this * b;}frac operator /= (const frac &b){return *this = *this / b;}};int main(){    int n;    while(~scanf("%d",&n)){        frac ans[100];        ans[1]=frac(1,1);        for(int i=2;i<=n;i++){            ans[i]=ans[i-1]+frac(1,i);        }        ans[n]*=frac(n,1);        ll a,b,c;        a=ans[n].up/ans[n].low;        b=ans[n].up%ans[n].low;        c=ans[n].low;        if(a&&b){            ll t=1;            while(a>=t){                printf(" ");                t*=10;            }            printf(" ");        }        if(b) printf("%lld\n",b);        if(a) printf("%lld",a);        if(b){            printf(" ");            ll t=1;            while(c>=t){                printf("-");                t*=10;            }        }        printf("\n");        if(a&&b){            ll t=1;            while(a>=t){                printf(" ");                t*=10;            }            printf(" ");        }       if(b)printf("%lld\n",c);    }    return 0;}




0 0
原创粉丝点击