-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path166.cpp
34 lines (34 loc) · 1.27 KB
/
166.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
class Solution {
string impl(long long numerator, long long denominator) {
auto result = numerator / denominator;
if (result * denominator == numerator)
return to_string(result);
unordered_map<long long, long long> meet;
string res;
int pos = 0;
for (numerator -= result * denominator; numerator != 0;) {
do {
if (auto it = meet.find(numerator); it != meet.end()) {
res = res.substr(0, it->second) + '(' +
res.substr(it->second) + ')';
return to_string(result) + "." + res;
}
meet[numerator] = pos++;
numerator *= 10;
res += "0";
} while (numerator < denominator);
auto result = numerator / denominator;
res[res.size() - 1] += result;
numerator -= result * denominator;
}
return to_string(result) + "." + res;
}
public:
string fractionToDecimal(int numerator, int denominator) {
bool neg=false;
if((numerator<0&&denominator>0)||(numerator>0&&denominator<0)){
neg=true;
}
return (neg ? "-" : "") + impl(abs(numerator), abs(denominator));
}
};