Posted on 2009-04-24 10:31
superman 阅读(86)
评论(0) 编辑 收藏 引用 所属分类:
USACO
1 #include <map>
2 #include <iostream>
3
4 using namespace std;
5
6 string int2str(int n)
7 {
8 if (n == 0)
9 return string("0");
10 string s;
11 while (n)
12 {
13 s += (n % 10 + '0');
14 n /= 10;
15 }
16 for (unsigned i = 0; i < s.size() / 2; i++)
17 swap(s[i], s[s.size() - i - 1]);
18 return s;
19 }
20
21 int main()
22 {
23 freopen("fracdec.in", "r", stdin);
24 freopen("fracdec.out", "w", stdout);
25
26 int a, b, q, r;
27 map <int, int> rRec;
28
29 while (cin >> a >> b)
30 {
31 string ans = int2str(a / b) + '.';
32
33 a = a % b;
34 rRec[a % b] = ans.size();
35
36 int cnt = ans.size() + 1;
37 while (true)
38 {
39 a = a * 10;
40 q = a / b;
41 r = a % b;
42
43 ans += (q + '0');
44
45 if (r == 0 || rRec[r])
46 break;
47
48 rRec[r] = cnt++ ;
49
50 a = r;
51 }
52
53 if (r)
54 {
55 ans += ')';
56 ans.insert(rRec[r], "(");
57 }
58
59 for (int i = 0; i < ans.size(); i++)
60 {
61 cout << ans[i];
62 if ((i + 1) % 76 == 0)
63 cout << endl;
64 }
65
66 cout << endl;
67
68 rRec.clear();
69 }
70
71 return 0;
72 }
73