• 1088


    a x b = c

    将a,b,c用out统一输出

    AC代码:

    #include <cstdio>
    #include <string>
    using namespace std;
    long long gcd(long long x,long long y){
        if(x == 0)
            return y;
        if(y == 0)
            return x;
        if(x < 0)
            x = -x;
        if(y < 0)
            y = -y;
        long long max,min;
        max = x > y ? x : y;
        min = x > y ? y : x;
        while(max % min != 0){
            long long tmp(min);
            min = max % min;
            max = tmp;
        }
        return min;
    }
    void out(long long an,long long ad){
        if(ad == 0){
            printf("Inf");
            return;
        }
        if(ad < 0){
            ad = -ad;
            an = -an;
        }
        long long ai,g;
        ai = an / ad;
        an = an - ai * ad;
        g = gcd(an,ad);
        an /= g;
        ad /= g;
        if(ai != 0){
            an = an > -an ? an : -an;
            if(an != 0){
                if(ai < 0){
                    printf("(%lld %lld/%lld)",ai,an,ad);
                }
                else{
                    printf("%lld %lld/%lld",ai,an,ad);
                }
            }
            else{
                if(ai < 0){
                    printf("(%lld)",ai);
                }
                else
                    printf("%lld",ai);
            }
        }
        else{
            if(an == 0)
                printf("0");
            else if(an < 0)
                printf("(%lld/%lld)",an,ad);
            else
                printf("%lld/%lld",an,ad);
        }
    }
    int main(){
        long long an,ad,bn,bd;
        scanf("%lld/%lld %lld/%lld",&an,&ad,&bn,&bd);
        out(an,ad);
        printf(" + ");
        out(bn,bd);
        printf(" = ");
        out(an*bd+bn*ad,ad*bd);
        printf("
    ");
    
        out(an,ad);
        printf(" - ");
        out(bn,bd);
        printf(" = ");
        out(an*bd-bn*ad,ad*bd);
        printf("
    ");
    
        out(an,ad);
        printf(" * ");
        out(bn,bd);
        printf(" = ");
        out(an*bn,ad*bd);
        printf("
    ");
    
        out(an,ad);
        printf(" / ");
        out(bn,bd);
        printf(" = ");
        out(an*bd,ad*bn);
        printf("
    ");
    
        return 0;
    }
  • 相关阅读:
    设计模式-代理模式
    设计模式-桥接模式
    设计模式-组合模式
    设计模式-享元模式
    设计模式-适配器模式
    设计模式-装饰器模式
    设计模式-外观模式
    redis日志格式
    Linux下的文件切割和文件合并
    Windows server 服务器的端口突然远程连不上了,但是可以远程连接,怎么回事?
  • 原文地址:https://www.cnblogs.com/Aldorado/p/5263412.html
Copyright © 2020-2023  润新知