zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<stack>
    #include<queue>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    struct FenShu
    {
        long long fz,fm;
        FenShu(long long a,long long b)
        {
            fz=a;
            fm=b;
        }
    };
    
    long long gcd(long long a,long long b)
    {
        if(b==0) return a;
        return gcd(b,a%b);
    }
    
    FenShu ADD(FenShu a,FenShu b)
    {
        FenShu res(0,1);
        res.fz=a.fz*b.fm+b.fz*a.fm;
        res.fm=a.fm*b.fm;
    
        if(res.fz!=0)
        {
            long long GCD=gcd(abs(res.fz),abs(res.fm));
            res.fz=res.fz/GCD;
            res.fm=res.fm/GCD;
        }
        else
        {
            res.fz=0;
            res.fm=1;
        }
        return res;
    }
    
    int main()
    {
        int n; scanf("%d",&n);
        FenShu ans(0,1);
        for(int i=1;i<=n;i++)
        {
            long long fz,fm; scanf("%lld/%lld",&fz,&fm);
            FenShu t(fz,fm);
            ans=ADD(ans,t);
        }
        //printf("%d/%d
    ",ans.fz,ans.fm);
        if(ans.fz==0) printf("0
    ");
        else
        {
            if(ans.fz%ans.fm==0) printf("%lld
    ",ans.fz/ans.fm);
            else if(abs(ans.fz)<ans.fm) printf("%lld/%lld
    ",ans.fz,ans.fm);
            else
            {
                long long d=ans.fz/ans.fm;
                ans.fz=ans.fz-d*ans.fm;
                printf("%lld %lld/%lld
    ",d,ans.fz,ans.fm);
            }
        }
        return 0;
    }
  • 相关阅读:
    Java异常处理和设计
    一次qps测试实践
    Alternate Task UVA
    Just Another Problem UVA
    Lattice Point or Not UVA
    Play with Floor and Ceil UVA
    Exploring Pyramids UVALive
    Cheerleaders UVA
    Triangle Counting UVA
    Square Numbers UVA
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5636195.html
Copyright © 2011-2022 走看看