http://poj.org/problem?id=1930
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
http://www.hankcs.com/program/cpp/poj-1930-dead-fraction.html
赶论文:论文里无限循环小数到底是由哪两个数除出来的呢?求分母最小的那一对。
2.6 数学问题的解题窍门
小学奥数水平的无聊题目,没做过无限循环小数的奥数题也无妨,看看百度百科里的公式就可以套出来:
纯循环
用9做分母,有多少个循环数就几个9,比如0.3,3的循环就是9分之3,0.654,654的循环就是999分之654, 0.9,9的循环就是9分之9(1),以此类推。混循环
先来看几个例子例:把混循环小数0.228˙化为分数:解:0.228˙=[(228/1000)+8/9000)]=228/(900+100)+8/9000=[(228/900)-(228/9000)]+(8/9000)=(228/900)+[(8/9000)-(228/9000)]=(228/900)-(22/900)=(228-22)/900=206/900=103/450;例:把混循环小数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,以此类推,能约分的要化简。
关键点在于题目在玩文字游戏,循环小数到底从哪一位开始没有明说,所以无脑暴力穷举一遍就行了。
typedef
unsigned
long
long
ULL;
ULL gcd(ULL a, ULL b)
{
if
(b == 0)
return
a;
return
gcd(b, a%b);
}
ULL ten_pow(unsigned
int
x)
{
ULL result = 1;
for
(unsigned
int
i = 0; i < x; ++i)
{
result *= 10;
}
return
result;
}
///////////////////////////SubMain//////////////////////////////////
int
main(
int
argc,
char
*argv[])
{
#ifndef ONLINE_JUDGE
freopen
(
"in.txt"
,
"r"
, stdin);
freopen
(
"out.txt"
,
"w"
, stdout);
#endif
string line;
while
(cin >> line)
{
if
(line ==
"0"
)
{
break
;
}
string digit = line.substr(2, line.length() - 5);
unsigned
int
length = digit.length();
const
ULL n =
atoi
(digit.c_str());
if
(n == 0)
{
cout <<
"0/1"
<< endl;
continue
;
}
ULL min_x = numeric_limits<ULL>::max();
ULL min_y = 0;
for
(
int
i = 1; i <= length; ++i)
{
string first = digit.substr(0, length - i);
ULL x = ten_pow(length) - ten_pow(length - i);
ULL y = n -
atoi
(first.c_str());
ULL d = gcd(x, y);
x /= d;
y /= d;
if
(min_x > x)
{
min_x = x;
min_y = y;
}
}
cout << min_y <<
'/'
<< min_x << endl;
}
#ifndef ONLINE_JUDGE
fclose
(stdin);
fclose
(stdout);
system
(
"out.txt"
);
#endif
return
0;
}
http://blog.csdn.net/xinghongduo/article/details/6231107
题意:很有意思的一道题,,将一个无限循环小数转化成分母最小的精确分数值....
(注意:循环的部分不一定是最后一位,有可能从小数点后面全是循环部分...我因为这个问题WA了2次,题意模糊)
思路:先推导一个数学等式
推导出上述等式,就可以在知道循环部分和非循环后计算出其精确分数值...所以只需要枚举这个数的末端,将其作为循环部分,其余为非循环部分即可计算出分母最小的精确分数值....
- int gcd(int a,int b)
- {
- if(!a)
- return b;
- return gcd(b%a,a);
- }
- int main()
- {
- char str[100]; int num,k,all,a,b,i,j,mina,minb,l;
- while(cin>>str&&strcmp(str,"0"))
- {
- mina=minb=1000000000;
- for(i=2,all=0,l=0;str[i]!='.';i++)
- {
- all=all*10+str[i]-48;
- l++;
- }
- for(num=all,k=1,i=1;i<=l;i++)
- {
- num/=10;
- k*=10;
- a=all-num;
- b=(int)pow(10.0,l-i)*(k-1);
- j=gcd(a,b);
- if(b/j<minb)
- {
- mina=a/j; minb=b/j;
- }
- }
- cout<<mina<<'/'<<minb<<endl;
- }
- return 0;
- }