LeetCode (29) Fraction to Recurring Decimal

题目描述

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)”.

解决这题需要注意的问题是,当余数开始重复的时候,得到的小数点后位数同样开始重复。

例如: 1/7=0.(142857);

  • 1/7=0;1mod7=1; 余数为1;此时字符串为 “0.”
  • 10/7=1;10mod7=3;余数为3; 此时字符串为 “0.1”
  • 30/7=4;30mod7=2;余数为2; 此时字符串为 “0.14”;
  • 20/7=2;20mod7=6;余数为6; 此时字符串为 “0.142”
  • 60/7=8;60mod7=4;余数为4; 此时字符串为 “0.1428”
  • 40/7=5;40mod7=5;余数为3; 此时字符串为 “0.14285”
  • 50/7=7;50mod7=1;余数为3; 此时字符串为 “0.142857”

此时得到余数为1,余第一个的余数重复,因此在余数为1得到的结果的数字处开始重复,即从字符串的第三位重复到目前字符串的最后一位,得到结果1/7=0.(142857);。在完成本题的过程中,只用记录每个余数第一次出现的位置即可。每次运算后判断余数是否已经出现,若已出现,找到该余数出现的第一个位置,也就是开始循环的位置。

代码如下:

class Solution {
public:
    string fractionToDecimal(int numerator, int denominator) {
        if (!numerator) return "0";
        string result;

        if ( numerator < 0 ^ denominator < 0) result += "-";

        long long int n = numerator, d = denominator;
        n = abs(n);
        d = abs(d);

        result += to_string(n / d);

        long long int r = n % d;
        if (!r) return result;
        else result += ".";

        unordered_map<int, int> reminder;
        while (r)
        {
            if (reminder.find(r) != reminder.end())
            {
                result.insert(reminder[r], 1, ‘(‘);
                result += ")";
                break;
            }

            reminder[r] = result.size();

            r *= 10;
            result += to_string(r / d);
            r = r % d;
        }

        return result;
    }
};
时间: 2024-10-08 13:35:23

LeetCode (29) Fraction to Recurring Decimal的相关文章

【LeetCode】Fraction to Recurring Decimal

Fraction to Recurring Decimal 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 numera

[LeetCode#116]Fraction to Recurring Decimal

Problem: 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

【LeetCode】Fraction to Recurring Decimal【Solution】

[题目] 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,

[leetcode] 166. Fraction to Recurring Decimal 解题报告

题目链接: https://leetcode.com/problems/fraction-to-recurring-decimal/ 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 p

Java for LeetCode 166 Fraction to Recurring Decimal

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, retu

[LeetCode]58. Fraction to Recurring Decimal分数化小数

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, retu

[LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数

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. Example 1: Input: numerator = 1, denominator = 2 Output

LeetCode – Refresh – Fraction to Recurring Decimal

Notes: 1. When numerator is 0, return "0". Check this corner case, because 0 / -5 will return -0. 2. Use long long int for divd and divs, mainly for divs. Because when divs = INT_MIN, fabs(divs) is large. 3. Do not forget to rest *= 10 to get th

leetcode[166] Fraction to Recurring Decimal

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)". 按照上例子实现结果.主要就是如何处理循环的数字. 我们发现如果循环了,那么余数也是出现了循环,所以从余数入手,一