#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
#define ll long long
ll gcd(ll a,ll b)
{
if(b==0) return a;
return gcd(b,a%b);
}
void output(ll a,ll b)
{
ll f=gcd(a,b);
if(b==0)
{
cout<<"Inf";
return;
}
if(a==0)
{
cout<<"0";
return;
}
a/=f;
b/=f;
if((a<0&&b>0)||(b<0&&a>0))
{
cout<<"(-";
if(a<0)
a=-a;
else if(b<0)
b=-b;
if(a%b==0)
{
cout<<a/b;
}
else if(a<b)
{
cout<<a<<'/'<<b;
}
else
{
cout<<a/b<<' '<<a%b<<'/'<<b;
}
cout<<")";
}
else
{
if(a%b==0)
{
cout<<a/b;
}
else if(a<b)
{
cout<<a<<'/'<<b;
}
else
{
cout<<a/b<<' '<<a%b<<'/'<<b;
}
}
}
int main()
{
ll a,b,c,d;
scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
output(a,b);
cout<<" + ";
output(c,d);
cout<<" = ";
output(a*d+c*b,b*d);
cout<<endl;
output(a,b);
cout<<" - ";
output(c,d);
cout<<" = ";
output(a*d-c*b,b*d);
cout<<endl;
output(a,b);
cout<<" * ";
output(c,d);
cout<<" = ";
output(a*c,b*d);
cout<<endl;
output(a,b);
cout<<" / ";
output(c,d);
cout<<" = ";
output(a*d,b*c);
cout<<endl;
return 0;
}
1088. Rational Arithmetic (20)
最新推荐文章于 2020-07-17 09:57:49 发布