标签:
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,
Credits:
Special thanks to @Shangrila for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
class Solution { public: string fractionToDecimal(int numerator, int denominator) { //防止INT_MIN取绝对值超出范围 long long num = numerator, den = denominator; if (den == 0) return ""; bool isNegative = (num > 0 && den < 0) || (num < 0 && den > 0); num = num > 0 ? num : -num; den = den > 0 ? den : -den; // 整数部分 string res = ""; res += (isNegative ? "-" : "") + to_string(num / den); long long rem = num % den; //注意极小数除以极大数时余数可能超出int范围 if (rem == 0) return res; else res += "."; // 小数部分 unordered_map<int, int> m; while (rem != 0) { if (m.find(rem) != m.end()) { res.insert(m[rem], 1, ‘(‘); res += ")"; break; } m[rem] = res.size(); // 方便插入左括号 rem *= 10; res += to_string(rem / den); rem %= den; } return res; } };
小数转分数:不循环小数转分数很简单,乘以10的幂将小数部分去掉即可;无限循环小数转分数分两种:一是循环节从小数点后第一位开始的,比如0.333(3/9-->1/3)...、0.7272(72/99-->8/11)...,循环节当做分子,循环节有几位就拿几个9当做分母再约分即可;另一种循环节不从小数点后第一位开始,比如0.437272...,乘以100得到43.7272...,可以理解为43+8/11即481/11,前面乘了100因此再除以100,即是481/1100。
[LeetCode]58. Fraction to Recurring Decimal分数化小数
标签:
原文地址:http://www.cnblogs.com/aprilcheny/p/4940242.html