various situations. There are disgusting negative numbers the most value, with a long long to do. In addition to the situation below are listed here.
/*1, 8 = 0.1251, 6 = 0.1 (6) -50, 6 = -6.250,-3 = 0-1, -2147483648 = "0.0000000004656612873077392578125" */typedef long Long Llong;class Solution {public:string fractiontodecimal (int numerator, int denominator) {if (numerator = = 0) { return "0"; The string result; Llong n = numerator; Llong d = denominator; if (n < 0 ^ D < 0) result+= '-'; n = ABS (n); D = ABS (d); Result + = to_string (n/d); Llong r = n% d; if (r = = 0) {return result; } else {result + = '. '; } unordered_map<int, int> map; while (r! = 0) {if (Map.find (r) = Map.end ()) {Result.insert (Map[r], 1, ' ('); Result + = ') '; Break } Map[r] = Result.size (); R *= 10; Result + = to_string (r/d); R%= D; } return result; }};
[leetcode]fraction to recurring Decimal