POJ 1930 Dead Fraction 无限循环小数化分数 数论+枚举

题目链接

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

题目大意:
给出一个形如"0.xxx…"的无限循环小数,把它转化成分母最小的分数。

解题思路:
做这道题需要一定的知识。。。网上都说这是小学奥数水平的题目。。。

纯循环:
把循环节作为分子,循环节有几位数在分母就写几个9;
比如0.33333…,3是循环节,分数便是9分之3;0.654654…,654是循环节,结果就是999分之654,以此类推。
混循环
先来看几个例子
例:把混循环小数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
公式
(小数部分(由非循环节部分与一个循环节组成)-小数非循环节部分)/999…000(9的个数是循环节部分的位数,0的个数是非循环节部分的位数)。

具体证明可以参考下面的大神文章:
如何把无限循环小数弄成分数?

由于题目中没有说明循环体从哪一位开始,所以我们需要枚举一遍,求出分母最小的那个分数。

AC代码:

#include<iostream>
#include<string>
using namespace std;
#define INF 0x3fffffff

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}
string t;
int fenzi(int a, int b) {//把[a,b]区间内的数字当作循环体
	int ans = 0; int ans1 = 0;
	for (int i = 2; i < a; i++) {
		ans = ans * 10 + (t[i] - '0');
	}
	ans1 = ans;
	for (int j = a; j <= b; j++) {
		ans = ans * 10 + (t[j] - '0');
	}
	return ans - ans1;
}
int main() {
	while (cin >> t) {
		int ans_fenzi = INF, ans_fenmu = INF;
		if (t == "0")break;
		int len = t.size() - 3;//去掉字符串后面的“...”
		for (int i = len - 1; i >= 2; i--) {//i要大于等于2是因为小数的最前面两位是“0.”
			int aa = fenzi(i, len - 1);
			int bb = 0;
			for (int j = i; j <= len - 1; j++) {
				bb = bb * 10 + 9;
			}
			for (int j = 2; j < i; j++) {
				bb *= 10;
			}
			//cout << a << " " << b << endl;
			int tmp = gcd(aa, bb);
			//cout << tmp << endl;
			//cout << a << " " << b << endl;
			aa = aa / tmp;
			bb = bb / tmp;
			if (bb < ans_fenmu) {
				ans_fenzi = aa;
				ans_fenmu = bb;
			}
		}
		cout << ans_fenzi << "/" << ans_fenmu << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值