UVa-202 - Repeating Decimals

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int a=n/m,b=n%m;
        int ans[5000]={a},cnt=0;
        int p[5000]={};
        p[b]=++cnt;
        while(1)
        {
            b*=10;
            ans[cnt]=b/m;
            b=b%m;
            if(p[b]) break;
            p[b]=++cnt;
        }
        cout<<n<<"/"<<m<<" = ";
        for(int i=0;i<=cnt&&i<p[b]+50;i++)
        {
            if(i==1) cout<<".";
            if(i==p[b]) cout<<"(";
            cout<<ans[i];
        }
        if(cnt>50) cout<<"...)"<<endl;
        else cout<<")"<<endl;
        cout<<"   "<<cnt-p[b]+1<<" = number of digits in repeating cycle"<<endl<<endl;
    }
}

posted on 2015-02-23 18:03  windrises  阅读(108)  评论(0编辑  收藏  举报

导航