Dead Fraction
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3101 | Accepted: 1017 |
Description
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).
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
There are several test cases. For each test case there is one line of input of the form "0.dddd..." where dddd is a string of 1 to 9 digits, not all zero. A line containing 0 follows the last case.
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...).
给出一个纯小数,它是一个无限循环小数的前一段,保证给出的部分中包含完整的循环节。要求将这个小数还原位无线循环小数,取分母最小的那个。
用9和0做分母,首先有一个循环节有几位数字就几个9,接着有几个没加入循环的数就加几个0,再用第二个循环节以前的小数部分组成的数与小数部分中不循环部分组成的数的差做分子,比如0.43,3的循环,有一位数没加入循环,就在9后面加一个0做分母,再用43减4做分子,得 90分之39,0.145,5的循环就用9后面加2个0做分母,再用145减14做分子,得900分之131,0.549,49的循环,就 用99后面加1个0做分母,用549减5做分子,最后得990分之545,以此类推,能约分的要化简。
这个题还有一个小坑就是它没有说明循环节是几位,也就是说要枚举循环节。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
char x[20];
//获得指定的一位数保留权值
int pow (int a, int b)
{
for(int i= 0; i< b; i++)
a *= 10;
return a;
}
//获取非循环节部分的数
int getc (int a, int x)
{
//排除循环节部分
while(x--) a /= 10;
int temp = 0;
for(int i= 0; ; i++)
{
if(a == 0) break;
temp += pow(a%10, i);
a /= 10;
}
return temp;
}
//获取分母
int getfm (int i, int len)
{
int j, temp = 0;
//按照循环节的位数添9
for(j= 0; j< i; j++)
temp = temp * 10 + 9;
//按照非循环节的位数添0
for( ; j< len; j++)
temp = temp * 10;
return temp;
}
//欧几里得法求最大公约数
int gcd (int x, int y)
{
if(y == 0) return x;
return gcd(y, x%y);
}
int main ()
{
while(1)
{
int p = 0, q = inf;
scanf(" %s", x);
if(strlen(x) == 1) break;
//提取小数部分a, len为小数部分的长度
int a = 0, len = 0;
for(int i= 2; x[i]!= '.'; i++)
a = a * 10 + x[i] - '0', len ++;
for(int i= 1; i<= len; i++)
{
int c = getc(a, i);
int fm = getfm(i, len);
int fz = a - c;
int g = gcd(fz, fm);
fm = fm / g;
//取分母最小的解
if(fm < q) q = fm, p = fz / g;
}
printf("%d/%d\n", p, q);
}
return 0;
}