Problem H: 2.4.5 Fractions to Decimals 分数化小数

永不放弃,永不放弃又有两个原则,第一个原则是永不放弃,第二个原则就是:当你想放弃时回头看第一个原则。


Description

写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式。 如果小数有循环节的话,把循环节放在一对圆括号中。例如, 1/3 = .33333333 写成0.(3) 41/333 = 0.123123123... 写成0.(123) 用xxx.0 成表示整数 典型的转化例子: 1/3 = 0.(3) 22/5 = 4.4 1/7 = 0.(142857) 2/2 = 1.0 3/8 = 0.375 45/56 = 0.803(571428)

Input

单独的一行包括被空格分开的 N和D, 1 <= N,D <= 100000。

Output

小数的表示方法上面说的很明白了,如果输出的长度超过76个字符,每行输出76个。

Sample Input

45 56

Sample Output

0.803(571428)

源代码:

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
     int a,c,m,n,y,f[200001],t1,t2,t3=0,t;
    long int x;
    cin>>m>>n;
    if(n!=0){
    y=1;
    if(m==n) cout<<"1.0"<<endl;
   
  if(m>n)
    {
         y=1;
        while(m!=0) //m>n输出
        {
            a=m/n;
            cout<<a;
            t3++;
            m=m%n;
            if(m<n&&m!=0&&y==1)
            {
                y=0;
                cout<<".";
            }
            if(m<n) break;
            m=m*10;
        }
    }
 if (m<n) //m<n输出
    {
        x=1;
       if(y==1) 
       {
           cout<<"0.";
           t3+=2;
       }
       else t3+=2;
        t1=m;
        f[0]=m;
        //str=m*10/n+48;
        while(x!=0)
        {//t4=m;
        m=(m*10)%n;
        f[x]=m;
    /*  if(x==73||(x-73)%76==0) 
        {
            str+='\n';
        }*/
        if(x<=100) t=x;
        else t=100;
        for(a=0;a<t;a++)
            if(f[a]==f[x])//当余数第二次出现
            {
                y=1;
                    while(t1!=m)//输出循环体之前的
                {
                    t2=t1*10/n;
                     cout<<t2;
                    t1=t1*10%n;
                }
                    if(m==0)    x=0;
                    if(m!=0) //输出循环体
                    {
                cout<<"(";
   
          // str=str.substr(a,x);
                //  cout<<str;
                   
               
                do
                {
                c=m*10/n;
              cout<<c;
                if((y+a+t3==75)||(y+a+t3-75)%76==0) cout<<endl;//换行
                y++;
                m=m*10%n;
                }
                while(m!=f[x]);//当余数第三次出现,停止
               
                cout<<")"<<endl;
                    x=0;
                    break;
                    }
            }
           // str+=m*10/n+48;
            if(x==0) break;
        x++;
        }
    }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值