forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_simulation_m2.cpp
67 lines (56 loc) · 1.42 KB
/
AC_simulation_m2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_m2.cpp
* Create Date: 2015-04-07 21:04:50
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
string ret;
long long num = numerator, den = denominator;
if (num * den < 0)
ret += "-";
num = abs(num);
den = abs(den);
// integer
char integer[100];
sprintf(integer, "%lld", num / den);
ret += integer;
// fractional
num %= den;
if (num == 0)
return ret;
ret += ".";
string frac;
vector<long long> rec;
rec.push_back(num);
while (true) {
for (int i = 0; i + 1 < rec.size(); ++i) {
if (rec[i] == num) {
// found
ret += frac.substr(0, i) + "(" + frac.substr(i) + ")";
return ret;
}
}
num *= 10;
long long d = num / den;
frac += '0' + d;
num -= den * d;
if (num == 0)
return ret + frac;
rec.push_back(num);
}
// will never go here
}
};
int main() {
int a, b;
Solution s;
while (cin >> a >> b)
cout << s.fractionToDecimal(a, b) << endl;
return 0;
}