Fractions to Decimals

Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation. If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as 0.(3), and 41/333 = 0.123123123...is denoted as 0.(123). Use xxx.0 to denote an integer. Typical conversions are:

1/3     =  0.(3)
22/5    =  4.4
1/7     =  0.(142857)
2/2     =  1.0
3/8     =  0.375
45/56   =  0.803(571428)

PROGRAM NAME: fracdec

INPUT FORMAT

A single line with two space separated integers, N and D, 1 <= N,D <= 100000.

SAMPLE INPUT (file fracdec.in)

45 56

OUTPUT FORMAT

The decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.

SAMPLE OUTPUT (file fracdec.out)

0.803(571428)

思路:找循环节,恶心的输出!!!!
Executing...
   Test 1: TEST OK [0.000 secs, 4008 KB]
   Test 2: TEST OK [0.000 secs, 4008 KB]
   Test 3: TEST OK [0.000 secs, 4008 KB]
   Test 4: TEST OK [0.000 secs, 4008 KB]
   Test 5: TEST OK [0.000 secs, 4008 KB]
   Test 6: TEST OK [0.000 secs, 4008 KB]
   Test 7: TEST OK [0.000 secs, 4008 KB]
   Test 8: TEST OK [0.000 secs, 4008 KB]
   Test 9: TEST OK [0.000 secs, 4008 KB]

All tests OK.
  1 /*
  2 ID:wuhuaju2
  3 PROG:fracdec
  4 LANG:C++
  5 */
  6 
  7 #include <cstdio>
  8 #include <iostream>
  9 #include <cstdlib>
 10 #include <algorithm>
 11 #include <cstring>
 12 using namespace std;
 13 
 14 const int maxn=100010;
 15 int mod,d,x,y,cnt,t,pos,tot;
 16 char a[maxn],ans[maxn],c[maxn];
 17 int b[maxn];
 18 bool f[maxn],flag;
 19 
 20 void close()
 21 {
 22     fclose(stdin);
 23     fclose(stdout);
 24     exit(0);
 25 }
 26 
 27 void work()
 28 {
 29     while (1)
 30     {
 31         t=x;
 32         x*=10;
 33         mod =x % y;
 34         x=x / y;
 35         if (f[t]==true || mod==0)
 36         {
 37             if (mod==0)
 38             {
 39                 cnt++;
 40                 a[cnt]=x+48;
 41                 flag=true;
 42             }
 43             break;
 44         }
 45         cnt++;
 46         a[cnt]=x+48;
 47         b[cnt]=t;
 48         f[t]=true;
 49         x=mod;
 50     }
 51     /*
 52     printf("t:%d\n",t);
 53     for (int i=1;i<=cnt;i++)
 54         cout<<b[i]<<" ";
 55     cout<<endl;
 56     */
 57     for (int i=1;i<=cnt;i++)
 58         if (t==b[i])
 59         {
 60             pos=i;
 61             break;
 62         }
 63     if (flag)
 64     {
 65     for (int i=1;i<=cnt;i++)
 66         printf("%c",a[i]);
 67     cout<<endl;
 68     close();
 69     }
 70     tot=0;
 71     for (int i=1;i<=cnt;i++)
 72     {
 73         if (pos==i)
 74         {
 75             tot++;
 76             ans[tot]='(';
 77         }
 78         tot++;
 79         ans[tot]=a[i];
 80     }
 81     tot++;
 82     ans[tot]=')';
 83     for (int i=1;i<=tot;i++)
 84     {
 85         printf("%c",ans[i]);
 86         if (i % 76==0)
 87             printf("\n");
 88     }
 89     cout<<endl;
 90 }
 91 
 92 void init ()
 93 {
 94 freopen("fracdec.in","r",stdin);
 95 freopen("fracdec.out","w",stdout);
 96    scanf("%d%d",&x,&y);
 97     int d=x/y;
 98     x=x-d*y;
 99     cnt=0;
100     while (1)
101     {
102         mod=d % 10;
103         d=d / 10;
104         cnt++;
105         c[cnt]=mod+48;
106         if (d==0)
107             break;
108     }
109     for (int i=1;i<=cnt;i++)
110         a[i]=c[cnt-i+1];
111     cnt++;
112     a[cnt]='.';
113 }
114 
115 int main ()
116 {
117     init();
118     work();
119     close();
120     return 0;
121 }

 

posted on 2013-01-31 09:01  cssystem  阅读(440)  评论(0)    收藏  举报