天天看點

POJ 1930 Dead Fraction【數學】

Dead Fraction

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2463 Accepted: 802

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
      

Hint

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

Source

Waterloo local 2003.09.27

題意:

英語實在不行,題意都沒讀明白,隻知道是把小數變成分數,但是卻沒發現題目都給出循環節,需要自己枚舉去找.........

另外輸出的是所有的可能性中最簡形式下分母最小的那一項...

題解:

循環小數化簡為分數的題解請點此處

唯一的變化,就是外加一層循環,枚舉循環節開始的地點,并記錄出現的最簡形式的分母的最小值

注意題目中的某個坑,題目說好的沒這樣的情況,結果...

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
	if(!b)
	{
		return a;
	}
	return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
	return a/gcd(a,b)*b;
}
int zero(char s[],int len)
{
	for(int i=2;i<len-3;++i)
	{
		if(s[i]!='0')
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	char s[105];
	while(~scanf("%s",s))
	{
		if(s[0]=='0'&&s[1]==0)
		{
			break;
		}
		ll len=strlen(s);
		if(zero(s,len))//玩這樣的坑有意思嗎? 
		{
			printf("0/1\n");
			continue;
		}
		ll ansx,ansy=1<<31-1;
		for(ll pos=2;pos<len-3;++pos)
		{
			ll a=0,x=1;//不循環部分 
			for(ll i=2;i<pos;++i)
			{
				a=a*10+s[i]-'0';
				x*=10;
			}
			ll b=0,y=0;//循環部分 
			for(ll i=pos;i<len-3;++i)
			{
				b=b*10+s[i]-'0';
				y=y*10+9;
			}
			y*=x;
			ll ty=lcm(x,y),tx=a*(ty/x)+b*(ty/y);
			ll tp=gcd(tx,ty);
			tx/=tp;ty/=tp;
			if(ty<ansy)
			{
				ansx=tx;ansy=ty;
			}
		}
		printf("%lld/%lld\n",ansx,ansy);
	}
	return 0;
}