zoukankan      html  css  js  c++  java
  • poj1930 数论

    Dead Fraction
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 1258   Accepted: 379

    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

    题意:最后一位表示循环节,
     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 
     7 #define inf 1000000000
     8 #define ll long long
     9 
    10 using namespace std;
    11 char ch[10005];
    12 ll ans1,ans2;
    13 ll gcd(ll a,ll b)
    14 {
    15     return b==0?a:gcd(b,a%b);
    16 }
    17 void solve(ll a,ll b,ll c,ll d)
    18 {
    19     ll t1=a*d+b*c,t2=b*d,t=gcd(t1,t2);
    20     t1/=t;t2/=t;
    21     if(t2<ans2)ans2=t2,ans1=t1;
    22 }
    23 int main()
    24 {
    25     while(~scanf("%s",ch+1))
    26     {
    27         ans2=(ll)1e60;
    28         int n=strlen(ch+1);
    29         if(n==1)break;
    30         ll b=1,a=0;
    31         for(int i=3;i<=n-3;i++)
    32             a=a*10+ch[i]-'0',b*=10;//三个. 
    33         ll t=b/10*9;
    34         for(ll i=10;i<=b;i*=10,t=t+(b/i)*9)
    35             solve(a/i,b/i,a%i,t);
    36         printf("%lld/%lld
    ",ans1,ans2);
    37     }
    38 }
     
     
  • 相关阅读:
    django第八天总结
    获取文件名的基本信息
    单个文件上传与多个文件上传
    return .php
    upload.php
    string.php
    upload.php
    upload.html
    获取上传文件
    那些年被我坑过的Python——牵一发动全身 第十一章MySQL、ORM
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7656287.html
Copyright © 2011-2022 走看看