zoukankan      html  css  js  c++  java
  • POJ 1930 Dead Fraction

    要输出分母最小的分数。

    所以需要枚举一下哪一个位置是循环的起点。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    long long a,b;
    long long f1,f2;
    long long ans1,ans2;
    char s[1000];
    
    long long gcd(long long a, long long b)
    {
        if (a%b == 0) return b;
        return gcd(b, a%b);
    }
    
    
    int main()
    {
        while(1)
        {
            scanf("%s",s);
            int len=strlen(s);
            if(len==1&&s[0]=='0') break;
            ans2=99999999999999;
            int pos;
            for(int i=len-1;i;i--) if(s[i]!='.'){pos=i;break;}
    
            for(int i=2;i<=pos;i++)
            {
                a=b=0; f1=1; f2=0;
                for(int j=2;j<=i-1;j++)
                {
                    a=a*10+(long long)(s[j]-'0');
                    f1=f1*10;
                }
                for(int j=i;j<=pos;j++)
                {
                    b=b*10+(long long)(s[j]-'0');
                    f2=f2*10+9;
                }
                f2=f2*f1;
    
                long long fz=a*f2+b*f1;
                long long fm=f1*f2;
    
                if(fm/gcd(fz,fm)<ans2)
                {
                    ans1=fz/gcd(fz,fm);
                    ans2=fm/gcd(fz,fm);
                }
            }
            printf("%lld/%lld
    ",ans1,ans2);
        }
        return 0;
    }
  • 相关阅读:
    053364
    053363
    oracle导出批量表N行记录
    053362
    053361
    053360
    053359
    053358
    053357
    053356
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5373436.html
Copyright © 2011-2022 走看看