码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-11-19 17:03:45      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:cout   cin   struct   ||   --   space   style   需要   using   

题意:

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

trick:

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

代码:

#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==0)
x=a/b;
if(c%d==0)
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<0)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" + ";
if(y<1e9){
if(y<0)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
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==0){
long long z=ab/cd;
if(z<0)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<0&&cd>0||ab>0&&cd<0){
if(ab<0)
ab=-ab;
if(cd<0)
cd=-cd;
long long yushu=0;
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=0;
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<0)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" - ";
if(y<1e9){
if(y<0)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
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==0){
long long z=ab/cd;
if(z<0)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<0&&cd>0||ab>0&&cd<0){
if(ab<0)
ab=-ab;
if(cd<0)
cd=-cd;
long long yushu=0;
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=0;
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<0)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" * ";
if(y<1e9){
if(y<0)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
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==0){
long long z=ab/cd;
if(z<0)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<0&&cd>0||ab>0&&cd<0){
if(ab<0)
ab=-ab;
if(cd<0)
cd=-cd;
long long yushu=0;
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=0;
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<0)
cout<<"("<<x<<")";
else
cout<<x;
}
else{
long long ta=a,tb=b;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb;
}
}
cout<<" / ";
if(y<1e9){
if(y<0)
cout<<"("<<y<<")";
else
cout<<y;
}
else{
long long ta=c,tb=d;
if(ta<0&&tb>0||ta>0&&tb<0){
if(ta<0)
ta=-ta;
if(tb<0)
tb=-tb;
long long yushu=0;
if(ta>tb){
yushu=ta/tb;
ta-=yushu*tb;
}
cout<<"(-";
if(yushu)
cout<<yushu<<" ";
cout<<ta<<"/"<<tb<<")";
}
else{
long long yushu=0;
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==0){
cout<<"Inf";
return 0;
}
if(ab%cd==0){
long long z=ab/cd;
if(z<0)
cout<<"("<<z<<")";
else
cout<<z;
}
else if(ab<0&&cd>0||ab>0&&cd<0){
if(ab<0)
ab=-ab;
if(cd<0)
cd=-cd;
long long yushu=0;
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=0;
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 0;
}

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

标签:cout   cin   struct   ||   --   space   style   需要   using   

原文地址:https://www.cnblogs.com/ldudxy/p/11890333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!