-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path166 Fraction to Recurring Decimal .cpp
44 lines (44 loc) · 1.57 KB
/
166 Fraction to Recurring Decimal .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
class Solution {
public:
string fractionToDecimal(int numeratorInt, int denominatorInt) {
string res;
auto myabs = [] (long long x) {
return x > 0 ? x : -x;
};
long long numerator = numeratorInt, denominator = denominatorInt;
if (numerator * denominator < 0) {
res += "-";
numerator = myabs(numerator);
denominator = myabs(denominator);
}
res += to_string(numerator / denominator);
if (numerator % denominator) {
res.push_back('.');
map<long long, int> remainders;
vector<long long> quotients;
long long remainder = numerator % denominator;
while (remainder && !remainders.count(remainder)) {
auto quotient = remainder * 10 / denominator;
remainders[remainder] = quotients.size();
quotients.push_back(quotient);
remainder = remainder * 10 % denominator;
}
if (!remainder) {
for (auto quotient : quotients) {
res += to_string(quotient);
}
} else {
int start = remainders[remainder];
for (int i = 0; i < start; i++) {
res += to_string(quotients[i]);
}
res += "(";
for (int i = start; i < quotients.size(); i++) {
res += to_string(quotients[i]);
}
res += ")";
}
}
return res;
}
};