YT14-HDU-Eddy的彩票

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;
}
时间: 2024-10-13 23:09:41

YT14-HDU-Eddy的彩票的相关文章

hdu Eddy&#39;s picture (最小生成树)

Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 29   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Eddy begins to like pa

HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5783    Accepted Submission(s): 3180 Problem Description The digital root of a positive integer is found by summing the digit

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1210 Eddy&#39;s 洗牌问题

Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如果他有2N张牌,编号为1,2,3..n,n+1,..2n.这也是最初的牌的顺序.通过一次洗牌可以把牌的序列变为n+1,1,n+2,2,n+3,3,n+4,4..2n,n.那么可以证明,对于任意自然数N,都可以在经过M次洗牌后第一次重新得到初始的顺序.编程对于小于100000的自然数N,求出M的值. Input 每行一个整数N Output 输出与之对应的M Sa

HDU 1165 Eddy&#39;s research II

Eddy's research II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3970    Accepted Submission(s): 1459 Problem Description As is known, Ackermann function plays an important role in the sphere

hdu 2200 Eddy&#39;s AC难题(简单数学。。)

题意: N个人,每个人AC的题数都不一样. Eddy想从中选出一部分人(或者全部)分成两组.必须满足第一组中的最小AC数大于第二组中的最大AC数. 问共有多少种不同的选择方案. 思路: 简单数学.. 代码: ll C(int n,int x){ ll ans=1; rep(i,1,x){ ans = ans*(n+1-i)/i; } return ans; } int main(){ int n; while(cin>>n){ ll ans = 0; rep(i,2,n){ ans += (C

HDU 2204 Eddy&#39;s爱好(容斥原理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2204 解题报告:输入一个n让你求出[1,n]范围内有多少个数可以表示成形如m^k的样子. 不详细说了,自己一开始也忽略了三个素数的乘积的乘方的情况. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6

hdu 1164 Eddy&#39;s research I

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 #define MAX 65535 9 int prime[MAX+5]; 10 bool vis[MAX+5]; 11 int index=0; 1

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdu 1162 Eddy&#39;s picture(最小生成树算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6866    Accepted Submission(s): 3469 Problem Description Eddy begins to like p