-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractionToRecurringDecimal.cpp
More file actions
86 lines (78 loc) · 2.15 KB
/
FractionToRecurringDecimal.cpp
File metadata and controls
86 lines (78 loc) · 2.15 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
void print(vector<int> &v){
for(auto i:v){
cout << i << " ";
}
cout << endl;
}
class Solution {
public:
void toDecimal(long long numerator, long long denominator, string &str, map<long long, int> &appear, int index){
// cout << numerator << " " << denominator << endl;
if(numerator == 0)
return;
appear[numerator] = index;
numerator *= 10;
long long d = numerator/denominator;
str.push_back(d+'0');
long long m = numerator%denominator;
map<long long, int>::iterator iter = appear.find(m);
if(iter == appear.end())
toDecimal(numerator%denominator, denominator, str, appear, index+1);
else{
str.insert(iter->second, 1, '(');
str.push_back(')');
}
}
string fractionToDecimal(int num, int denom) {
if(num == 0)
return "0";
long long numerator = num;
long long denominator = denom;
int sign = 1;
if(numerator < 0){
numerator = -numerator;
sign = -sign;
}
if(denominator < 0){
denominator = -denominator;
sign = -sign;
}
long long d = numerator/denominator;
char buf[32];
sprintf(buf, "%ld", d);
long long m = (numerator+denominator)%denominator;
// cout << d << endl;
map<long long, int> appear;
string fraction_part;
toDecimal(m, denominator, fraction_part, appear, 0);
string result;
if(sign == -1)
result.push_back('-');
result.append(buf);
if(m != 0)
result.push_back('.');
result.append(fraction_part);
return result;
}
};
int main(int argc, char *argv[]) {
Solution sol;
// cout << sol.fractionToDecimal(0, 2) << endl;
// cout << sol.fractionToDecimal(1, 2) << endl;
// cout << sol.fractionToDecimal(1, 9) << endl;
// cout << sol.fractionToDecimal(10, 1) << endl;
// cout << sol.fractionToDecimal(1, 7) << endl;
// cout << sol.fractionToDecimal(3, 72) << endl;
// cout << sol.fractionToDecimal(-2147483648, -1) << endl;
// cout << sol.fractionToDecimal(-50, 8) << endl;
// cout << sol.fractionToDecimal(7, -12) << endl;
cout << sol.fractionToDecimal(-1, -2147483648) << endl;
return 0;
}