One of the count, if there is a repetition of the remainder of the cycle is the beginning of the festival.
Use Hashtable to record the position and insert parentheses.
Note negative numbers.
classSolution { Public: stringFractiontodecimal (intNumerator,intdenominator) { intSIGN1 = Numerator >=0?1: -1; intSIGN2 = Denominator >=0?1: -1; Long Longnum = (Long Long) numerator; Long LongDen = (Long Long) denominator; Num=ABS (num); Den=abs (DEN); Long Longd = num/den; Long Longrem = num%den; Unordered_map<Long Long,int>Rec; stringAns =to_string (d); if(SIGN1 * Sign2 = =-1&& (d >0|| rem >0)) ans ="-"+ans; if(REM = =0)returnans; Ans= ans +"."; stringR =""; for(intpos =0; Rem! =0; pos++) { if(Rec.find (REM)! =Rec.end ()) {R.insert (Rec[rem],"("); R= R +")"; returnAns +R; } Rec[rem]=POS; R= R + to_string ((REM *Ten) /den); REM= (REM *Ten) %den; } returnAns +R; }};
[Leetcode] Fraction to recurring Decimal