POJ

来源:互联网 发布:便宜的鼠标 知乎 编辑:程序博客网 时间:2024/06/03 16:50

题目链接:http://poj.org/problem?id=1930点击打开链接

Dead Fraction
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 3242 Accepted: 1068

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/91/51186531/2500000


后面的小数点不一定是最后一位的 可能是倒数两位 三位。。等等

比如0.123...

可能是0.1233333   0.1232323  0.123123123

然后让你计算分母最小的精度与所给的值相等的分数


完全不会啊 数学渣渣 借鉴别人的博客才大致了解思路

枚举每一种小数点的情况

然后将小数点前的确定精度转化为小数点前的数

然后通过精度差可以计算小数点后所表示的分数 然后比较正确精度的分母大小即可

#include <iostream>#include <vector>#include <algorithm>#include <math.h>#include <stdio.h>#include <cstring>#include <limits.h>using namespace std;int gcd(int a,int b){    if(!a)        return b;    return gcd(b%a,a);}int main(){    char  s[111];    while(scanf(" %s",s)&&strcmp(s,"0"))    {        int num=0;int len=0;int mina=INT_MAX,minb=INT_MAX;        for(int i=2;s[i]!='.';i++)        {            num=num*10+s[i]-'0';            len++;        }        int mid=num;        for(int i=1;i<=len;i++)        {            mid/=10;            int a=num-mid;            int b=(int)pow(10.0,len-i)*((int)pow(10,i)-1);            int j=gcd(a,b);            if(b/j<minb)            {                mina=a/j;                minb=b/j;            }        }        printf("%d/%d\n",mina,minb);    }}