一:问题描述
Description: As we know, a rational number is a number that can be represented as y/x where x and y are integers and y is not equal to zero. On the hand, a rational number could also be represented in decimal form and the resulting decimal is a finite decimal or an infinite repeating decimal. Now, you are given the x and y , please return the decimal form of y/x in string format, and enclose the repeating part for the infinite repeating decimal in parentheses, for example:
X=1, Y=2, return ‘0.5’
X=2, Y=3, return ‘0.(6)’
X=7, Y=15, return ‘0.4(6)’
二:斟酌题意:
其实就是将 一个 分数用 小数表示,如果是无限循环小数,那么用括号将循环节 括起来。事例已经说明这点。
三:思路以及方法
不妨先假设分子为26,分母为23。根据此实例加以分析。
我们先计算整数部分 pre = 26/23 ,小数部分 为 分子 numerator = 26 % 23 =3,分母denominator为 23。我们循环计算余数 remainder[i] = numerator *10 % denominator ,如