PAT 1034. 有理数四则运算
PAT 1034. 有理数四则运算
本题要求编写程序,计算2个有理数的和、差、积、商。
输入格式:
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。
输出格式:
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。
输入样例1:
2/3 -4/2
输出样例1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
输入样例2:
5/3 0/6
输出样例2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
分析
自己写的程序如下,测试点2未过,如有大神路过,请指点一二,万分感谢
自己的代码
#include<iostream>
#include<math.h>
using namespace std;
long long int inf=0,flag3=1,int3,son3,mom3;
void print(long long int flag,long long int Int,long long int son,long long int mom){
if(inf==1){
cout<<"Inf"; return;
}
if(flag==0)
cout<<0;
else if(flag<0){
if(Int==0)
cout<<"("<<-son<<"/"<<mom<<")";
else if(Int!=0&&son!=0)
cout<<"("<<-Int<<" "<<son<<"/"<<mom<<")";
else
cout<<"("<<-Int<<")";
}
else if(flag>0){
if(Int==0) cout<<son<<"/"<<mom;
else if(Int!=0&&son!=0)cout<<Int<<" "<<son<<"/"<<mom;
else cout<<Int;
}
flag3=1;
}
long long int gcd(long long int t1,long long int t2) {
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void huajian(long long int &son,long long int &mom,long long int &Int,long long int &flag){
long long int s=abs(son);
long long int temp=gcd(s,mom);
son/=temp; mom/=temp;
if(son<0) {flag=-1; son=-son;}
else if(son==0) flag=0;
Int=son/mom; son=son%mom;
}
void jia(long long int s1,long long int m1,long long int s2,long long int m2){
mom3=m1*m2;
son3=m1*s2+m2*s1;
}
void jian(long long int s1,long long int m1,long long int s2,long long int m2){
mom3=m1*m2;
son3=s1*m2-s2*m1;
}
void chen(long long int s1,long long int m1,long long int s2,long long int m2){
mom3=m1*m2;
son3=s1*s2;
}
void chu(long long int s1,long long int m1,long long int s2,long long int m2){
if(s2==0) inf=1;
else{
son3=s1*m2;
mom3=s2*m1;
}
if(son3*mom3>0) {son3=abs(son3); mom3=abs(mom3); }
else if(son3*mom3<0){son3=-abs(son3); mom3=abs(mom3);}
}
int main(){
long long int mom1,son1,int1,mom2,son2,int2,flag1=1,flag2=1;
scanf("%lld/%lld %lld/%lld",&son1,&mom1,&son2,&mom2);
char fuhao[4]={'+','-','*','/'};
long long int m1=mom1,s1=son1,m2=mom2,s2=son2;
huajian(son1,mom1,int1,flag1);
huajian(son2,mom2,int2,flag2);
for(int i=0;i<4;i++){
print(flag1,int1,son1,mom1);
cout<<" "<<fuhao[i]<<" ";
print(flag2,int2,son2,mom2);
cout<<" = ";
switch(i){
case 0:jia(s1,m1,s2,m2); break;
case 1:jian(s1,m1,s2,m2); break;
case 2:chen(s1,m1,s2,m2); break;
case 3:chu(s1,m1,s2,m2); break;
default:;
}
huajian(son3,mom3,int3,flag3);
print(flag3,int3,son3,mom3);
cout<<endl;
}
return 0;
}
目前看过的本题最好的代码
#include<iostream>
#include<math.h>
using namespace std;
long long int getgcd(long long int s,long long int m){
int r;
while(r=s%m){
s=m;
m=r;
}
return m;
}
void print(long long int s,long long int m){
int sign=1;
if(m==0){
cout<<"Inf";
return;
}
if(s<0){
s=abs(s);
sign*=-1;
}
if(m<0){
m=abs(m);
sign*=-1;
}
int gcd=getgcd(s,m);
s/=gcd;
m/=gcd;
if(sign<0) cout<<"(-";
if(m==1) cout<<s;
else if(s>m) cout<<s/m<<" "<<s%m<<"/"<<m;
else cout<<s<<"/"<<m;
if(sign<0) cout<<")";
}
int main(){
long long int s1,m1,s2,m2;
char op[4]={'+','-','*','/'};
scanf("%lld/%lld %lld/%lld",&s1,&m1,&s2,&m2);
for(int i=0;i<4;i++){
print(s1,m1);
cout<<" "<<op[i]<<" ";
print(s2,m2);
cout<<" = ";
switch(i){
case 0:print(s1*m2+s2*m1,m1*m2); break;
case 1:print(s1*m2-s2*m1,m1*m2); break;
case 2:print(s1*s2,m1*m2); break;
case 3:print(s1*m2,m1*s2); break;
}
cout<<endl;
}
return 0;
}