usaco Fractions to Decimals

这是一道模拟题。

/*
ID: guo geer
PROG: fracdec
LANG: C++
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

int note[300000];

int f(int n)
{
    int k=0;
    if(n==0) return 1;
    while(n)
    {
            n/=10;
            k++;
    }
    return k;
}

int main()
{
    freopen("fracdec.in","r",stdin);
    freopen("fracdec.out","w",stdout);
    int m,n;
    while(scanf("%d%d",&m,&n)==2)
    {
          if(m%n==0)
          {
                    printf("%d.0\n",m/n);
                    continue;
          }                 
          memset(note,0,sizeof(note));     
          int t=m%n;        
          for(int i=0;;i++)
          {
                  note[t]=1;
                  t=(t*10)%n;
                  if(note[t]==1)break;
          }
          int Count=f(m/n);
          printf("%d.",m/n);
          m=m%n;
          Count ++;
          int times = 0;
          for(int i=0;;i++)
          {               
                  if(m==t)
                  {
                          if(t==0) break;

                          if(times == 0)
                          {
                               printf("(");
                               Count++;
                               if(Count%76==0) printf("\n"); 
                          }
                          if(times == 1)
                          {
                               printf(")");
                               Count++;
                               break;   
                          }
                          times ++;                         
                  }

                  printf("%d",(m*10)/n);
                  m=(m*10)%n;
                  Count++;
                  if(Count%76==0) printf("\n");                  
          }
          printf("\n");               
    }
    return 0;
}


你可能感兴趣的:(c)