POJ-1930 Dead Fraction(简单数论)

来源:互联网 发布:王津钟表修复知乎 编辑:程序博客网 时间:2024/05/14 15:38

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by “…”. For instance, instead of “1/3” he might have written down “0.3333…”. Unfortunately, his results require exact fractions! He doesn’t have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions. To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes cow i’s milking interval with two
space-separated integers.

output

For each case, output the original fraction.

Sample Input

0.2…
0.20…
0.474612399…
0

Sample Output

2/9
1/5
1186531/2500000

Hint

Note that an exact decimal fraction has two repeating expansions (e.g.
1/5 = 0.2000… = 0.19999…).

给定出一个无限循环小数,不给定循环节,要求写出其分母最小的分数形式。
枚举循环节,然后求分数形式,找出分母最小的分数输出。

循环小数化分数求法:

eg1

设无限循环小数 x=0.222222… (2为循环节) ——A
即有10*x=2.22222…… ——B
A、B的小数部分相同
B-A=10*x-x=2.2222…-0.2222…=2
即x=2/(10-1)=2/9(能约分就约分)

eg2

设有无限循环小数 x=0.738292323…(23为循环节)
即有 10^7*x=7382923.23… (7=A中非循环部分是5位+循环节是2位) ——B
10^5*x=73829.2323… ——A
A、B的小数部分相同
B-A=10^7*x-10^5*x=7382923.23…-73829.2323…=7382923-73829
=73829*100-73829+23=73829*99+23
即10^5(100-1)x=99*10^5*x=73829*99+23
x=(73829*99+23)/99*10^5 (嗷,记得约分)

综上有公式

分子:不循环部分*10^(循环节数字个数)+循环节
分母:10^(不循环部分数字个数)*(10^(循环节数字个数)-1)【或者循环节有几个数字就有几个9 如上例 99(2个9)=10^2-1】
记得要约分 !

0 0
原创粉丝点击