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 分)【难度: 简单 / 知识点: 模拟】的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。