1088 Rational Arithmetic (20 分)难度: 简单 / 知识点: 模拟

Posted 辉小歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1088 Rational Arithmetic (20 分)难度: 简单 / 知识点: 模拟相关的知识,希望对你有一定的参考价值。


https://pintia.cn/problem-sets/994805342720868352/problems/994805378443755520
PAT乙级的原题,直接模拟即可

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL a,b,c,d;
LL gcd(LL a,LL b) {return b?gcd(b,a%b):a;}
void print(LL a,LL b)
{
    LL temp=gcd(abs(a),abs(b));
    int flag=1;//记录数的正负
    if(a<0) flag=flag*(-1),a=abs(a);
    if(b<0) flag=flag*(-1),b=abs(b);
    a/=temp,b/=temp;
    LL cnt=a/b;
    a=a-cnt*b;
    if(cnt==0&&a==0) flag=1;
    if(flag<0) cout<<"(";
    if(a==0) printf("%lld",cnt*flag);
    else if(cnt&&a) printf("%lld %lld/%lld",cnt*flag,a,b);
    else if(!cnt&&a) printf("%lld/%lld",a*flag,b);
    if(flag<0) cout<<")";
}
int main(void)
{
    scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    print(a,b),printf(" + "),print(c,d),printf(" = "),print(a*d+c*b,b*d);
    puts("");
    print(a,b),printf(" - "),print(c,d),printf(" = "),print(a*d-c*b,b*d);
    puts("");
    print(a,b),printf(" * "),print(c,d),printf(" = "),print(a*c,b*d);
    puts("");
    print(a,b),printf(" / "),print(c,d),printf(" = ");
    if(b*c!=0) print(a*d,b*c);
    else printf("Inf");
    return 0;
}

以上是关于1088 Rational Arithmetic (20 分)难度: 简单 / 知识点: 模拟的主要内容,如果未能解决你的问题,请参考以下文章

A.1088 Rational Arithmetic (20)

A1088.Rational Arithmetic

1088 Rational Arithmetic

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

1088 Rational Arithmetic (20 分)难度: 简单 / 知识点: 模拟

PAT甲级1088 Rational Arithmetic (20 分)