您的位置:首页 > 其它

pat甲级1088. Rational Arithmetic (20)、乙级1034. 有理数四则运算(20)

2018-03-11 14:34 477 查看

甲级1088. Rational Arithmetic (20)

时间限制200 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.Input Specification:Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.Output Specification:For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 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

乙级1034. 有理数四则运算(20)
时间限制200 ms
内存限制65536 kB
代码长度限制8000 B
判题程序Standard作者CHEN, Yue
本题要求编写程序,计算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

C++代码:

#include<bits/stdc++.h>
using namespace std;
struct Fraction{
//虽然题目中说明分子分母都在整型范围内,
//但是在四则运算过程中很可能超出int范围,最好用long long存储
long long mother;
long long son;
};
//辗转相除法求最大公约数
long long gcd(long long a,long long b){
return b==0?a:gcd(b,a%b);
}
//约分
void simplify(Fraction&f){
if(f.son==0){
f.mother=1;
return;
}
if(f.mother<0){
f.mother=-f.mother;
f.son=-f.son;
}
long long d=gcd(max(abs(f.son),f.mother),min(abs(f.son),f.mother));
f.son/=d;
f.mother/=d;
}
//加法
Fraction add(Fraction&f1,Fraction&f2){
Fraction f3;
f3.son=f1.son*f2.mother+f1.mother*f2.son;
f3.mother=f1.mother*f2.mother;
simplify(f3);
return f3;
}
//减法
Fraction sub(Fraction&f1,Fraction&f2){
Fraction f3;
f3.son=f1.son*f2.mother-f1.mother*f2.son;
f3.mother=f1.mother*f2.mother;
simplify(f3);
return f3;
}
//乘法
Fraction mul(Fraction&f1,Fraction&f2){
Fraction f3;
f3.son=f1.son*f2.son;
f3.mother=f1.mother*f2.mother;
simplify(f3);
return f3;
}
//除法
Fraction div(Fraction&f1,Fraction&f2){
Fraction f3;
f3.son=f1.son*f2.mother;
f3.mother=f1.mother*f2.son;
simplify(f3);
return f3;
}
//输出分数
void output(Fraction&f){
if(f.son<0)
printf("(");
if(f.mother==1)
printf("%lld",f.son);
else{
if(abs(f.son)>=f.mother)
printf("%lld %lld/%lld",f.son/f.mother,abs(f.son)%f.mother,f.mother);
else
printf("%lld/%lld",f.son,f.mother);
}
if(f.son<0)
printf(")");
}

int main(){
Fraction f1,f2,f3;
scanf("%lld/%lld %lld/%lld",&f1.son,&f1.mother,&f2.son,&f2.mother);
simplify(f1);
simplify(f2);
for(int i=0;i<4;++i){
output(f1);
bool f=true;
if(i==0){
printf(" + ");
f3=add(f1,f2);
}else if(i==1){
printf(" - ");
f3=sub(f1,f2);
}else if(i==2){
printf(" * ");
f3=mul(f1,f2);
}else if(i==3){
printf(" / ");
if(f2.son==0)
f=false;
else
f3=div(f1,f2);
}
output(f2);
printf(" = ");
if(f)
output(f3);
else
printf("Inf");
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: