Dead Fraction
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1762 | Accepted: 568 |
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...).
Source
将分数分成循环的部分和非循环的部分
设分数为0.i1 i2 i3 i4 .. ik j1 j2 j3 .. jc 其中i1 ~ ik 为非循环的部分 j1 ~ jc为循环部分
非循环的部分可以拆成 b / a 其中 b = ( i1...ik) a = 10 ^ (k)
循环的部分可以拆成 bb / aa 其中 bb = (j1 .. jc) aa = 10 ^ (k + c) - 10 ^ ( k);
则 所求分数为 b / a + bb / aa 通分得 (b * aa + bb * a) / a * aa 约分得答案,由于据说数据会有全是0的坑爹数据,所以要判断一下
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 typedef long long ll; 9 10 char s[20]; 11 ll ten_pow[20]; 12 13 ll gcd(ll x,ll y) { 14 return y > 0 ? gcd(y,x % y) : x; 15 } 16 17 void init() { 18 ten_pow[0] = 1; 19 for(int i = 1; i <= 9; i++) { 20 ten_pow[i] = ten_pow[i - 1] * 10; 21 } 22 23 /*for(int i = 0; i <= 9; i++){ 24 printf("%lld ",ten_pow[i]); 25 26 }*/ 27 } 28 void solve() { 29 30 ll a,b,aa,bb; 31 ll ans1,ans2 = -1; 32 for(int i = 0; i < strlen(s) - 2; i++) { 33 b = 0; bb = 0; 34 for(int j = 2; j < 2 + i; j++) { 35 b += (s[j] - '0') * ten_pow[1 + i - j]; 36 } 37 for(int j = 2 + i; j < strlen(s); j++){ 38 bb += (s[j] - '0') * ten_pow[strlen(s) - 1 - j]; 39 } 40 41 42 a = ten_pow[i]; 43 aa = ten_pow[strlen(s) - 2] - ten_pow[i]; 44 // printf("b =%lld bb=%lld a = %lld aa = %lld ",b,bb,a,aa); 45 46 b = b * aa + bb * a; 47 a *= aa; 48 49 ll t = gcd(a,b); 50 b /= t; 51 a /= t; 52 53 //printf("a = %lld b = %lld ",a,b); 54 if(ans2 == -1 || ans2 > a) { 55 ans2 = a; 56 ans1 = b; 57 } 58 59 60 } 61 62 printf("%I64d/%I64d ",ans1,ans2); 63 64 65 66 } 67 68 int main() { 69 init(); 70 // freopen("sw.in","r",stdin); 71 72 while(~scanf("%s",s) && strlen(s) != 1) { 73 74 75 int pos = strlen(s) - 1; 76 while(s[pos] == '.') { 77 s[pos--] = '