166. Fraction to Recurring Decimal

problem

solution

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
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
int s1 = numerator >= 0 ? 1 : -1;
int s2 = denominator >= 0 ? 1 : -1;
long long num = abs( (long long)numerator );
long long den = abs( (long long)denominator );
long long out = num / den;
long long rem = num % den;
unordered_map<long long, int> m;
string ret = to_string(out);
if (s1 * s2 == -1 && (out > 0 || rem > 0)) ret = "-"+ret;
if(rem ==0) return ret;
ret+=".";

string s = "";
int pos = 0;
while(rem!=0){
if(m.find(rem) !=m.end()){
s.insert(m[rem],"(");
s+=")";
return ret+s;
}
m[rem] = pos;
cout<<rem<<" "<<pos<<endl;
s+= to_string((rem*10) / den);
rem = (rem*10) % den;
++pos;
}
return ret+s;
}
};