zoukankan      html  css  js  c++  java
  • poj1930(小数化分数)

    Dead Fraction
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions:3539   Accepted: 1180

    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
    题目大意是给你一个无限循环的小数,要你找到分母最小的分数,有一个坑点是你不知道从哪里开始这个小数部分开始循环,所以要暴力枚举一下。
    我们假设不循环的部分位数为k,循环部分位数为c,那么,让这个小数*pow(10,k+c),然后再减去pow(10,k),就可以把后面无限循环的部分减掉。
    设a=all-num,all=pow(10,k+c),num=pow(10,k),那么这个分数就可以表示为a/(pow(10,k+c)-pow(10,k)),求出上下两个的最大公约数进行约分就是最终结果。
    代码如下:
    #include<iostream>
    #include<math.h>
    using namespace std;
    //#define N 1000
    int gcd(int a,int b)
    {
        if(b==0)
        return a;
        return gcd(b,a%b);
    }
    int main()
    {
            char str[100];
            int all,num,i,l,k,minb,mina,a,b,j;
            while(cin>>str&&strcmp(str,"0"))
           {
           //int all=0,num=0,i,l=0,k=1,minb=10000000,mina=10000000;
           mina=minb=1000000000;
           for(i=2,all=0,l=0;str[i]!='.';i++)
           {
               //l++;
                all=all*10+str[i]-48;
                l++;
           }
           for(i=1,num=all,k=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)
               {
                   minb=b/j;
                   mina=a/j;
               }
           }
           cout<<mina<<'/'<<minb<<endl;
          }
         return 0;
    }












  • 相关阅读:
    滴滴打车如何成就150亿估值
    互联网专车高补贴开始“退烧”
    城市拥堵加剧,都是互联网快车惹的祸?
    滴滴打车动态加价10-20余元
    专车降价滴滴快车使命终结?
    “专车”监管意见最快本月公布
    专车新规或下周发布,估计有大量司机流失
    滴滴优步神州掀新一轮融资大战
    杭州或率先放开非公司化专车
    恭喜您!获得20元现金红包一个,赶快领取!
  • 原文地址:https://www.cnblogs.com/renxin123/p/8622124.html
Copyright © 2011-2022 走看看