HDU-博彩问题-带分数

79 篇文章 0 订阅
问题及代码:
/* 
*Copyright (c)2014,烟台大学计算机与控制工程学院 
*All rights reserved. 
*文件名称:number.cpp 
*作    者:单昕昕 
*完成日期:2015年2月3日 
*版 本 号:v1.0 
* 
*问题描述: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 consists of a sequence of lines each containing a single positive integer n, 1<=n<=22, giving the 

size of the set of coupons.
*程序输出: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

*/ 
//<span style="font-family: Arial, Helvetica, sans-serif;">AC代码。</span>
#include <iostream>
using namespace std;
int n,s,a1,b1,a2,b2,s1,s2;
int gcd(int a,int b)
{
    return (a%b!=0?(gcd(b,a%b)):b);//求最大公倍数
}
int 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;
}
//我写的这个代码<span style="font-family: Arial, Helvetica, sans-serif;">明明答案什么的都对,可就是WA了。。真心难过。。</span>
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
    return (a%b!=0?(gcd(b,a%b)):b);
}
int lcm(int u,int v)  //求最大公倍数
{
    int h;
    h=gcd(u,v);
    return(u*v/h);
}
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n],i;
        long long temp,s=0,m,fz,mm,ff,gys,count,flag;
        a[0]=0;
        for(i=1; i<=n; ++i)
            a[i]=i;
        temp=a[1];
        for(i=2; i<=n; i++)
            temp=lcm(temp,a[i]);
        for(i=1; i<=n; ++i)
            s+=(temp/i);
        s*=n;
        m=s/temp;
        mm=m;
        fz=s-(temp*m);
        gys=gcd(fz,temp);
        if(fz==0)cout<<m<<" "<<endl;
        else
        {
            for(count = 0; ; count++)
            {
                mm=mm/10;
                if(mm <= 0)
                {
                    break;
                }
            }
            for(i=0; i<=count+1; ++i)
                cout<<" ";
            flag=count;
            cout<<fz/gys<<endl;
            cout<<m<<" ";
            ff=temp/gys;
            for(count = 0; ; count++)
            {
                ff=ff/10;
                if(ff <= 0)
                {
                    break;
                }
            }
            for(i=0; i<=count; ++i)
                cout<<"-";
            cout<<endl;
            for(i=0; i<=flag+1; ++i)
                cout<<" ";
            cout<<temp/gys<<endl;
        }
    }
    return 0;
}



运行结果:


知识点总结:
就是求n*(1/1+1/2+1/3+...+1/n)这个式子的值,然后化成带分数。

学习心得:

格式输出很变态,一定要注意:

1、分数前面有个空格;

2、分数线的长度和分母一样;

3、分数线是由减号构成的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值