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

分析

这题的难点是如何找到无限循环的那一段。仔细回想一下人脑进行除法的过程,会发现,当一个余数第二次重复出现时,就说明小数点后开始无限循环了。

代码

results matching ""

    No results matching ""