USACO:Fractions to Decimals

/*
ID: Jang Lawrence
PROG: fracdec
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#define X first
#define Y second
#define sqr(a) ((a)*(a))
using namespace std;
typedef long long lng;
int x,y;
string an;
void f(int ini)
{
    if(ini>=10) f(ini/10);
    an+='0'+ini%10;
}
int  is[111111];
vector<int > ans;
int main()
{
  #ifndef  DEBUG
  freopen("fracdec.in","r",stdin);
  freopen("fracdec.out","w",stdout);
  #endif
scanf("%d%d",&x,&y);
f(x/y);
x=x%y;
bool f=1;
while(!is[x])
{
   is[x]=ans.size()+1;
   ans.push_back((x*10)/y);
   x=(x*10)%y;
   if(x==0){is[x]=ans.size();f=0;break;}
}
an+='.';
for(int i=0;i<is[x]-1;++i)
an+=ans[i]+'0';
if(f)an+='(';
for(int i=is[x]-1;i<ans.size();++i)
an+=ans[i]+'0';
if(f)an+=')';
for(int i=0,j=0;i<an.size();++i)
{
    if(j==76)
    {
        puts("");
        putchar(an[i]);
        j=1;
        continue;
    }
    else
    {
        putchar(an[i]);
        j++;
    }
}
if(an.size()%76==0) ;
else puts("");
    return 0;
}

你可能感兴趣的:(USACO:Fractions to Decimals)