【PAT甲级】1088 Rational Arithmetic (20 分)

时间:2021-12-09 17:46:28

题意:

输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式。小数需要用"("和")"括起来,分母为0的话输出"Inf"(输入的分母保证不为0)。

trick:

测试点2很容易溢出,建议遇到乘法时先化简分数并且不要用相乘小于零来判断异号。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long a,b,c,d;
cin>>a;
cin.ignore();
cin>>b>>c;
cin.ignore();
cin>>d;
long long x=1e9,y=1e9;
if(a%b==)
x=a/b;
if(c%d==)
y=c/d;
long long gcd=__gcd(a,b);
a/=gcd;
b/=gcd;
gcd=__gcd(c,d);
c/=gcd;
d/=gcd;
if(x<1e9){
if(x<)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" + ";
if(y<1e9){
if(y<)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" = ";
long long cd=b*d;
long long ab=a*d+c*b;
if(ab%cd==){
long long z=ab/cd;
if(z<)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<&&cd>||ab>&&cd<){
if(ab<)
ab=-ab;
if(cd<)
cd=-cd;
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd<<")";
}
else{
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd;
}
cout<<"\n";
//加减法分割线-------------
if(x<1e9){
if(x<)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" - ";
if(y<1e9){
if(y<)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" = ";
cd=b*d;
ab=a*d-c*b;
if(ab%cd==){
long long z=ab/cd;
if(z<)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<&&cd>||ab>&&cd<){
if(ab<)
ab=-ab;
if(cd<)
cd=-cd;
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd<<")";
}
else{
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd;
}
cout<<"\n";
//减法和乘除法分隔线-------------
if(x<1e9){
if(x<)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" * ";
if(y<1e9){
if(y<)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" = ";
cd=b*d;
ab=a*c;
if(ab%cd==){
long long z=ab/cd;
if(z<)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<&&cd>||ab>&&cd<){
if(ab<)
ab=-ab;
if(cd<)
cd=-cd;
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd<<")";
}
else{
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd;
}
cout<<"\n";
//乘除法分隔线-------------
if(x<1e9){
if(x<)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" / ";
if(y<1e9){
if(y<)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<&&tb>||ta>&&tb<){
if(ta<)
ta=-ta;
if(tb<)
tb=-tb;
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" = ";
cd=b*c;
ab=a*d;
if(cd==){
cout<<"Inf";
return ;
}
if(ab%cd==){
long long z=ab/cd;
if(z<)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<&&cd>||ab>&&cd<){
if(ab<)
ab=-ab;
if(cd<)
cd=-cd;
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd<<")";
}
else{
long long yushu=;
if(ab>cd){
yushu=ab/cd;
ab-=yushu*cd;
}
long long gcd=__gcd(ab,cd);
ab/=gcd;
cd/=gcd;
if(yushu)
cout<<yushu<<" ";
cout<<ab<<"/"<<cd;
}
return ;
}