POJ 1930 Dead Fraction(gcd—枚举循环节,无限循环小数变最简分数)
2016-01-14 21:06
441 查看
[align=center]Dead Fraction[/align]
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
Sample Output
Hint
Note that an exact decimal fraction has two repeating expansions (e.g. 1/5 = 0.2000... = 0.19999...).
题意:把无限循环小数变最简分数,但不确定循环节是哪几位,要求输出分母最小的分数。
题解:枚举循环节,然后不断更新最小的分母,同时记录下对应的分子即可。关于循环小数化分数,在之前的题解中给出过详细的转化方法及证明。链接:小数化分数
代码如下:
[align=center]Dead Fraction[/align]
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2323 | Accepted: 748 |
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...).
题意:把无限循环小数变最简分数,但不确定循环节是哪几位,要求输出分母最小的分数。
题解:枚举循环节,然后不断更新最小的分母,同时记录下对应的分子即可。关于循环小数化分数,在之前的题解中给出过详细的转化方法及证明。链接:小数化分数
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define ll __int64 using namespace std; ll gcd(ll n,ll m) { ll x; while(m) { x=n%m; n=m; m=x; } return n; } int main() { int len,i,j,point,k; ll inter; char str[20]; while(scanf("%s",str)&&strcmp(str,"0")!=0) { len=strlen(str); for(i=2;i<len;++i) { if(str[i]=='.') { point=i;//记录数字后面第一个点出现的位置 break; } } ll ans_num=0x3f3f3f3f,ans_cnt; for(i=2;i<point;++i) { j=i-2;//j表示小数点后非循环节的长度 ll cnt=0,num=0; for(k=i;k<point;++k) { cnt=cnt*10+(str[k]-'0');//分子 num=num*10+9;//分母 } inter=0; for(k=2;k<i;++k) inter=inter*10+(str[k]-'0');//循环节前面的数字 cnt+=inter*num; for(k=0;k<j;++k) num*=10; ll res=gcd(num,cnt); cnt/=res; num/=res; if(num<ans_num)//更新最小的分母 { ans_num=num; ans_cnt=cnt; } } printf("%I64d/%I64d\n",ans_cnt,ans_num); } return 0; }
相关文章推荐
- android初接触之service1
- 全面解析Linux 内核 3.10.x - initramfs 启动流程
- android 获取apk md5值
- Javascript图片预加载详解
- 颜色直方图匹配(一)
- Android studio ElasticDownloadView
- swift常用字符串处理
- 技术人才流向了哪里?
- CCS的知识点
- 16-Angular中的动画
- 如何查看javaAPI的源码?
- 中断下半部之工作队列
- swift常用字符串处理
- 【Jan.Challenge】持续时刻变化的线段树(某时刻领先选手的位置)
- Cheapest Palindrome(DP)
- JAVA 中文乱码的问题
- 关于maven传递性依赖和dependencyManagement
- Python 2.7.x 和 3.x 版本的重要区别小结
- FTP的主动模式和被动模式
- shell---practice4