输入输出细节处理,USACO Fractions to Decimals

算法还是很简单的,基本上学过小学除法算术的都知道,n/d的每位运算所得的余数只可能是0..d-1,如果在某处出现一个余数之前曾经出现过(在小数位上),那么可以肯定此时从该处到上次用出现这个这个商之间存在循环节。这样,就可以用基本的标记法就可以了。

所以这题其实麻烦的是输出的处理,又要加括号,又要求每行只能输出76个字符。麻烦!害得我又WA了两次,郁闷中。



/*
ID: fairyroad
TASK: fracdec
LANG: C++
*/
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("fracdec.in");
ofstream fout("fracdec.out");

bool existed[100001];
size_t index[100001];
vector<int> remainder;

inline size_t countp(int num)
{
    if(!num) return 1;
    int res = 0;
    while(num) ++res, num/=10;
    return res;
}

int main()
{
    int n, d;
    fin>>n>>d;
    int f = 0, cnt = 0;
    bool flag = true; // 是否能整除,与加括号有关
    if(n >= d){ f = n/d; n = n%d; }
    while(!existed[n])
    {
        existed[n] = true;
        index[n] = cnt++;
        remainder.push_back(n*10/d);
        if((n*10)%d==0){flag = false;break;}
        n = (n*10)%d;
    }

    fout<<f<<'.';
    size_t charnum = countp(f)+1;
    for(size_t i = 0; i < remainder.size(); ++i)
    {
        if(charnum == 76) fout<<endl, charnum = 0;
        if(i == index[n] && flag)  ++charnum, fout<<'(';
        fout<<remainder[i];
        ++charnum;
    }
    if(flag) fout<<')';
    fout<<endl;

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值