Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
For example,
Given numerator = 1, denominator = 2, return "0.5".
Given numerator = 2, denominator = 1, return "2".
Given numerator = 2, denominator = 3, return "0.(6)".
class Solution {public:string to_str(long long n){ string str=""; if(n==0) { str+='0'; return str; } long long tmp=n; long long power=1; int icount=0; while (tmp) { tmp/=10; power*=10; icount++; } power/=10; tmp=n; for (int i=0;i0&&den<0)||(num<0&&den>0)) res+="-"; num=num>=0?num:-num,den=den>=0?den:-den; long long part=num/den; res+=to_str(part); long long f=num%den; if(f==0)return res; res+='.'; map fmap; while(f) { if (fmap.count(f)) { int beg=fmap[f]; string part1=res.substr(0,beg); string part2=res.substr(beg,res.length()); res=part1+"("+part2+")"; return res; } else { fmap[f]=res.length(); f*=10; res+=to_str(f/den); f%=den; } } return res;}};