p1530 Fractions to Decimals

将余数记录下来,如果余数相同,那么商的下一位也相同。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-8;
typedef long long lon;
const lon SZ=100030,INF=0x7FFFFFFF,mod=9901;
int vst[SZ];

string work(int n,int d)
{
    if(n==0)return ".0";
    else
    {
        memset(vst,-1,sizeof(vst));
        string str=".";
        int res,rem;
        vst[n]=0;
        for(int i=1;;++i)
        {
            n*=10;
            str+=n/d+'0';
            n=n%d;
            //cout<<i<<" "<<n<<endl;
            if(vst[n]!=-1)
            {
                //cout<<vst[n]<<" "<<i<<endl;
                str=str.substr(0,vst[n]+1)+"("+str.substr(vst[n]+1,i-vst[n])+")";
                break;
            }
            vst[n]=i;
            if(n==0)break;
        }
        return str;
    }
}

string toStr(int x)
{
    if(x==0)return "0";
    string str="";
    for(;x;)
    {
        str=(char)(x%10+'0')+str;
        x/=10;
    }
    return str;
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //for(lon time=1;time<=casenum;++time)
    {
        int n,d;
        cin>>n>>d;
        string res=toStr(n/d)+work(n%d,d);
        for(int i=0;i<res.size();++i)
        {
            if(i%76==0&&i)cout<<endl;
            cout<<res[i];
        }
        //cout<<res<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/gaudar/p/9828504.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值