• PAT (Advanced Level) 1088. Rational Arithmetic (20)


    简单题。

    注意:读入的分数可能不是最简的。输出时也需要转换成最简。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<stack>
    #include<queue>
    #include<string>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    struct FS
    {
        long long fz,fm;
        FS(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);
    }
    
    FS change(FS res)
    {
        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;
    }
    
    FS ADD(FS a,FS b)
    {
        FS res(0,1);
    
        res.fz=a.fz*b.fm+b.fz*a.fm;
        res.fm=a.fm*b.fm;
    
        res=change(res);
        return res;
    }
    
    FS SUB(FS a,FS b)
    {
        FS res(0,1);
    
        res.fz=a.fz*b.fm-b.fz*a.fm;
        res.fm=a.fm*b.fm;
    
        res=change(res);
        return res;
    }
    
    FS MUL(FS a,FS b)
    {
        FS res(0,1);
    
        res.fz=a.fz*b.fz;
        res.fm=a.fm*b.fm;
    
        res=change(res);
        return res;
    }
    
    FS DIV(FS a,FS b)
    {
        FS res(0,1);
    
        if(b.fz==0)
        {
            res.fz=0;
            res.fm=0;
            return res;
        }
    
        if(a.fz==0) return res;
    
        res.fz=a.fz*b.fm;
        res.fm=a.fm*b.fz;
    
        if(res.fm<0)
        {
            res.fm=-res.fm;
            res.fz=-res.fz;
        }
    
        res=change(res);
        return res;
    }
    
    void output(FS a)
    {
        if(a.fm==0)
        {
            printf("Inf");
            return;
        }
    
        if(a.fz==0)
        {
            printf("0");
            return;
        }
    
        a=change(a);
        if(abs(a.fz)<a.fm)
        {
            if(a.fz<0) printf("(");
    
            printf("%lld/%lld",a.fz,a.fm);
            if(a.fz<0) printf(")");
            return;
        }
    
        if(a.fz>0)
        {
            if(a.fz%a.fm==0)
            {
                printf("%lld",a.fz/a.fm);
                return;
            }
            else
            {
                printf("%lld %lld/%lld",a.fz/a.fm,a.fz%a.fm,a.fm);
                return;
            }
        }
    
        else
        {
            a.fz=-a.fz;
    
            printf("(");
            if(a.fz%a.fm==0)
            {
                printf("-%lld",a.fz/a.fm);
                printf(")");
                return;
            }
            else
            {
                printf("-%lld %lld/%lld",a.fz/a.fm,a.fz%a.fm,a.fm);
                printf(")");
                return;
            }
    
        }
    }
    
    int main()
    {
        long long s1,s2,s3,s4;
        scanf("%lld/%lld %lld/%lld",&s1,&s2,&s3,&s4);
    
        FS a(s1,s2);
        FS b(s3,s4);
    
        output(a); cout<<" + "; output(b); cout<<" = "; output(ADD(a,b));
        cout<<endl;
        output(a); cout<<" - "; output(b); cout<<" = "; output(SUB(a,b));
        cout<<endl;
        output(a); cout<<" * "; output(b); cout<<" = "; output(MUL(a,b));
        cout<<endl;
        output(a); cout<<" / "; output(b); cout<<" = "; output(DIV(a,b));
        cout<<endl;
    
        return 0;
    }
  • 相关阅读:
    JVM(随手笔记)
    linux常用操作(个人笔记)
    MySQL学习笔记(个人随手笔记)
    jquery对象和Dom对象的转化([0])
    函数防抖和函数节流
    数据持久化分析
    day.js处理相对时间
    外链资源403的处理
    前端实现图片预览的两种方式及使用
    监听器标准写法
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5638731.html
Copyright © 2020-2023  润新知