模拟着算

写完 挂在第7组上 之后乱七八糟的改。。

终于刷完了第二章  刷了好久了。。偶尔想起来刷刷

USACO2.45Fractions to Decimals_#includeUSACO2.45Fractions to Decimals_c++_02View Code
 1 /*
 2        ID: shangca2
 3        LANG: C++
 4        TASK: fracdec
 5  */
 6 #include <iostream>
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<stdlib.h>
10 #include<algorithm>
11 using namespace std;
12 int d,di[100000],v[1000001],g,flag,w;
13 void judge(int m)
14 {
15     int k;
16     while(1)
17     {
18         m*=10;
19         di[++g] = m/d;
20         if(v[m])
21         {
22             flag = 1;
23             w = v[m];
24             break;
25         }
26         else
27         v[m] = g;
28         k = m%d;
29         if(k==0)
30         break;
31         m = k;
32     }
33 }
34 int main()
35 {
36     freopen("fracdec.in","r",stdin);
37     freopen("fracdec.out","w",stdout);
38     int i,j,k=0,n,m,o=0;
39     cin>>n>>d;
40     if(n>=d)
41     {
42         k = n/d;
43     }
44     int y = k;
45     if(y==0) o++;
46     while(y)
47     {
48         o++;
49         y/=10;
50     }
51     m = n%d;
52     if(m==0)
53     cout<<k<<"."<<"0\n";
54     else
55     {
56         judge(m);
57         cout<<k<<".";
58         o++;
59         if(flag)
60         {
61             for(i = 1; i < g ; i++)
62             {
63                 if(o%76==0)
64                 puts("");
65                 if(i==w)
66                 {
67                     cout<<"(";
68                     o++;
69                     if(o%76==0)
70                     puts("");
71                 }
72                 cout<<di[i];
73                 o++;
74             }
75             cout<<")";
76         }
77         else
78         for(i = 1; i <= g ; i++)
79         {
80             if(o%76==0)
81             puts("");
82             cout<<di[i];
83             o++;
84         }
85         puts("");
86     }
87     return 0;
88 }