天天看點

Dead Fraction POJ - 1930

Dead Fraction POJ - 1930 

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). 

      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.

      For each case, output the original fraction.

Sample Input

0.2...

0.20...

0.474612399...

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...). 

        英語太挫,看不懂題意(ORZ)

        題意:   将給定 的無限循環小數 變為  分數形式, 求分母最小的那種情況。

       思路:這個題跟擴歐就沒什麼關系了,直接考慮如何轉化。

       假設  一個小數為 0.abcd...ba... 即設前面非循環部分長度為  m,循環部分循環節為 n。原分數為  x;

       其實對于每個求無限循環小數轉 分數的做法都是 想辦法去掉  後面的  無限循環部分。

       那麼  x*10^(m+n)=abcdba.ba...

                x*10^m  = abcd.bababa...         

         x*10^(m+n)-x*10^m = abcdba-abcd .

         =x*10^m*(10^n-1)

         顯然 x= ( abcdba-abcd )/ ( 10^m*(10^n-1))

       對于這個題因為我們要找最小的分母的情況是以需要枚舉,枚舉循環節為 1~n  就行。

      (注意: pow 直接用庫函數的話,第三個樣例得出的結果不對,但是能A,最好還是自己寫一下 快速幂)

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
#define maxn 100
#define INF 0x3f3f3f3ff
#define eps 1e-8
#define MOD 1000000007
#define ll long long
using namespace std;


ll gcd(ll a,ll b)
{
    if(!b)
        return a;
    return  gcd(b,a%b);
}
ll pow(int n)
{
    ll res = 1;
    for (int i = 0; i < n; i++)
    {
        res *= 10;
    }
    return res;
}
int main()
{
    char str[maxn];
    while(cin>>str&&strcmp(str,"0"))
    {
        ll ans=0;
        int len=0;
        for(int i=2;str[i]!='.';i++)
        {
            len++;
            ans=ans*10+str[i]-'0';
        }
        ll a,b,k;
        ll mina,minb;
        mina=minb=INF;
        ll sum=ans;
        for(int i=1;i<=len;i++)
        {
            sum/=10;
            a=ans-sum;
            b=pow(len-i)*(pow(i)-1);
            k=gcd(b,a);
            if(minb>b/k)
            {
                mina=a/k;
                minb=b/k;
            }
        }
        printf("%lld/%lld\n",mina,minb);
    }
    return 0;
}