码迷,mamicode.com
首页 > 其他好文 > 详细

YT14-HDU-Eddy的彩票

时间:2015-02-06 09:35:12      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:c++   iostream   博客   namespace   编程   

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

代码如下:

#include <iostream>
using namespace std;
int n,s,a1,b1,a2,b2,s1,s2;
int gcd(int x,int y)
{
    int t;
    while (x%y!=0)
    {
        t=x%y;
        x=y;
        y=t;

    }
    return y;
}
void f(int x,int y)
{
    int t1=a1,t2=b1;
    a1=t1*y+b1*x;
    b1=t2*y;
    int t=a1/b1;
    s+=t;
    a1-=t*b1;
    t=gcd(a1,b1);
    a1=a1/t;
    b1=b1/t;
}

int main()
{
    while (cin>>n)
    {
        s=0;
        a1=0;
        b1=1;
        for (int i=1; i<=n; i++)
        {
            f(n,i);
        }
        if (a1==0)
            cout <<s<<endl;
        else
        {
            int t1=0,t2=0,temp1=s,temp2=b1;
            while (temp1!=0)
            {
                t1++;
                temp1/=10;
            }
            t1++;
            while (temp2!=0)
            {
                t2++;
                temp2/=10;
            }
            for (int i=1; i<=t1; i++)
                cout <<" ";
            cout <<a1<<endl;
            cout <<s<<" ";
            for (int i=t2; i>=1; i--)
                cout <<"-";
            cout <<endl;
            for (int i=1; i<=t1; i++)
                cout <<" ";
            cout <<b1<<endl;
        }
    }
    return 0;
}





YT14-HDU-Eddy的彩票

标签:c++   iostream   博客   namespace   编程   

原文地址:http://blog.csdn.net/liuchang54/article/details/43559593

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!