• poj 1930 Dead Fraction(循环小数化分数)


    Dead Fraction
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 3478   Accepted: 1162

    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

     
     题意:将一个无限循环小数化作分数
      这道题并没有告诉循环节是多少,并且让求分母最小的,所以暴力每个循环节
     1 #include <stdio.h>  
     2 #include <iostream>  
     3 #include <algorithm>  
     4 #include <string.h>  
     5 #include <math.h>   
     6 #include <string>  
     7 #include <vector>  
     8 #include <queue>   
     9 #include <stack>   
    10 #include <set>  
    11 #include <map>  
    12 using namespace std;
    13 typedef long long ll;
    14 const int MAXN = 105;
    15 const int INF = 0x3f3f3f3f;
    16 char s[MAXN];
    17 //欧几里得求最大公因数   
    18 int gcd(int x, int y)
    19 {
    20     if (x<y) swap(x, y);
    21     return y == 0 ? x : gcd(y, x%y);
    22 }
    23 //快速幂   
    24 int q_pow(int a, int b)
    25 {
    26     int r = 1, base = a;
    27     while (b) 
    28     {
    29         if (b & 1) r *= base;
    30         base *= base;
    31         b >>= 1;
    32     }
    33     return r;
    34 }
    35 int main(void)
    36 {
    37     while (scanf("%s", s) != EOF && strcmp(s, "0"))
    38     {
    39         int all = 0, cnt1 = 0;
    40         int len = strlen(s);
    41         for (int i = 2; i<len - 3; i++, cnt1++)
    42             all = all * 10 + s[i] - '0';
    43         //all为 非循环节和循环节连起来的数    
    44         int mina = INF, minb = INF; //所求的分子与分母   
    45         for (int num = all / 10, cnt2 = cnt1 - 1; cnt2 >= 0; num /= 10, cnt2--)
    46         {
    47             //num为非循环节部分连起来的数 ,a为当前循环节下的分子,b为当前循环节下的分母   
    48             int a = all - num, b = q_pow(10, cnt2)*(q_pow(10, cnt1 - cnt2) - 1);
    49             int g = gcd(a, b);
    50             //求出分母最小的   
    51             if (b / g<minb)
    52             {
    53                 minb = b / g;
    54                 mina = a / g;
    55             }
    56         }
    57         printf("%d/%d
    ", mina, minb);
    58     }
    59     return 0;
    60 }
    View Code
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <string>
    10 #include <cmath>
    11 using namespace std;
    12 const int INF=0x3f3f3f3f;
    13 typedef long long ll;
    14 int gcd(int n,int m)//求最大公约数
    15 {
    16     if(m==0) return n; //n%m==0(n与m的余数为0)
    17     return gcd(m,n%m);(n是大数,m是小数)
    18 }
    19 int main()
    20 {
    21     int all,num,l,m,n,a,b,k,mis,mns;
    22     char str[100];
    23     while(gets(str)&&strcmp(str,"0"))
    24     {
    25         l=0;all=0;mis=INF;
    26         for(int i=2;str[i]!='.';i++)
    27         {
    28             all=all*10+str[i]-48;
    29             l++;
    30         }
    31         num=all;
    32         for(int j=1;j<=l;j++)
    33         {
    34             num=num/10;
    35             a=all-num;
    36             b=(int)pow(10,l-j)*(pow(10,j)-1);
    37             k=gcd(b,a);
    38             if(b/k<mis)
    39             {
    40                 mns=a/k;
    41                 mis=b/k;
    42             }
    43         }
    44         printf("%d/%d
    ",mns,mis);
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    葡萄城报表介绍:数据报表的七个原则
    while(scanf("%d",&n)!=EOF)
    不容易系列之(3)—— LELE的RPG难题
    错排公式
    _​_​i​n​t​6​4​ ​与​l​o​n​g​ ​l​o​n​g​ ​i​n​t
    杭电ACM 2046 阿牛的EOF牛肉串
    C++Builder 中使用 __int64 整数的问题
    折线分平面——杭电2050
    复合梯形公式与复合辛普森公式求积分
    两套蓝桥杯热身题
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271192.html
Copyright © 2020-2023  润新知