Dead Fraction(辗转相除法)

原题链接Dead Fraction
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2668 Accepted: 867
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://poj.org/problem?id=1930
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=100 + 5;
char s[maxn];
int  gcd(int a,int  b){
        return b==0 ? a : gcd(b,a%b);
}
int main(){
        //freopen("1.txt","w",stdout);
        while(scanf("%s",s)==1){
                int loc=0,len=strlen(s);
                if(len==1){
                        if(s[0]=='0') break;
                        else printf("%d\n",s[0]-'0');
                }
                else{
                        int b=2,e=2;
                        while(s[e]!='.') e++;//这时例如0.289... b=2,e=5;
                        int len=e-b;
                        int minup=INF,minsum=INF;
                        for(int i=1;i<=len;i++){//列举所有的循环节
                                int sum=0;//sum是分母
                                int cishu=1;
                                //得到分母
                                for(int j=1;j<=i;j++){
                                        sum+=9*cishu;
                                        cishu*=10;
                                }
                                for(int j=1;j<=len-i;j++){
                                        sum*=10;
                                }
                                //得到分子
                                int up=0;
                                cishu =1;
                                for(int j=e-1;j>=b;j--){
                                        up+=(s[j]-'0')*cishu;
                                        cishu*=10;
                                }
                                cishu=1;
                                for(int j=e-1-i;j>=b;j--){
                                        up-=(s[j]-'0')*cishu;
                                        cishu*=10;
                                }
                                //上下进行约分
                                int _gcd=gcd(up,sum);
                                up/=_gcd;sum/=_gcd;
                                if(sum<minsum){
                                        minsum=sum;
                                        minup=up;
                                }
                        }
                        printf("%d/%d\n",minup,minsum);
                }
        }
        return 0;
}
/*
题解http://blog.csdn.net/u011008379/article/details/51220569
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值