题目来源 given two integers representing the n merator and denominator of a fraction, ret rn the fraction in string format. if the fractional part is repeating, enclose the repeating part in parentheses. for example, given n merator = 1, denominator = 2, ret rn "0.5". given n merator = 2, denominator = 1, ret rn "2". given n merator = 2, denominator = 3, ret rn "0.(6)". 怎么把分数写成循环小数的形式?想着先把整数求出来吧,假设分子是a,分母是b,那么整数部分就是ab,然后判断a%b是不是为0,不是的话怎么办呢?好好想一想! 思考中 ¥%...@%¥@¥¥#...@#¥%@¥¥%@#¥%%¥#...#¥% 然后判断是不是可以除尽,可以除尽的话直接输出,不能除尽的话找循环部分.怎么找循环部分呢?想不出来了... 好吧,我又看了大神们的做法,想想我们平时做除法是怎么做的,比如2除以3,2除以3余2,补0再除,余2,发现余数重复了,那么之后就会一直循环下去了对不对,对不对对不对!对啊,卧槽没想到啊,我个笨脑子! 然后一开始还得注意考虑下正负问题,考虑除数是0的问题,还得考虑除数或被除数是int_min的问题,所以得把类型定义为long long. class sol tion { p blic: string fractiontodecimal(int n merator, int denominator) { string res = ""; if (n merator == 0) ret rn "0"; if (n merator  0 ^ denominator  0) res += "-"; long long n = abs((long long)(n merator)); long long d = abs((long long)(denominator)); long long z = n  d; res += to_string(z); if (n % d == 0) ret rn res; else { nordered_mapint, int map; res += "."; for (long long r = n % d; r; r = r % d) { if (map[r]  0) { res.insert(map[r], 1, ( ); res += ")"; break; } map[r] = res.size; r *= 10; res += char(r  d + 48); } } ret rn res; } };