Dead Fraction
Time Limit: 1000MS | Memory Limit: 30000K |
Total Submissions: 2080 | Accepted: 661 |
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).
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
- - 话说真不会这东西~ 然后百度一发 - - 瞬间 这题真水。。。。
例:把混循环小数0.123˙68˙化成分数: 解:0.123˙68˙=(0.12368+0.00000˙68˙) =(12368/100000)+(68/9900000) =[(12368/99000)-(12368/990000)]+(68/9900000) =(12368/99000)+[(68/9900000)-(12368/9900000)] =(12368/99000)-(12300/9900000) =(12368-123)/99000 公式 用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,以此类推,能约分的要化简。
有个坑点 就是题目说不会全0 但是事实上 有全0的 case。。。。
AC代码如下:
//
// POJ 1930 Dead Fraction
//
// Created by TaoSama on 2015-03-24
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
string s;
int pow(int n) {
int ret = 1;
for(int i = 1; i <= n; ++i)
ret *= 10;
return ret;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(cin >> s) {
if(s == "0") break;
s = s.substr(2, s.size() - 5);
int n = atoi(s.c_str()), sz = s.size();
if(n == 0) cout << "0/1" << endl;
else {
int ansx, ansy = INF;
for(int i = 1; i <= sz; ++i) {
string xx = s.substr(0, sz - i);
int x = n - atoi(xx.c_str());
int y = pow(sz) - pow(sz - i);
int gcd = __gcd(x, y);
x /= gcd; y /= gcd;
if(y < ansy) ansx = x, ansy = y;
}
cout << ansx << '/' << ansy << endl;
}
}
return 0;
}