1034 有理数四则运算 (20 point(s))
本题要求编写程序,计算 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
1、C
//PAT1034V1
#include
#include
void Addition(long long,long long, long long ,long long);
void Subtract(long long,long long,long long ,long long );
void Multiply(long long,long long,long long ,long long );
void Divide(long long,long long,long long ,long long );
long long gcd(long long ,long long );
//辗转相除法求最大公约数
long long gcd(long long a,long long b){
if(b==0) return a; //可以用条件表达式表达 return b==0?a:gcd(b,a%b)
return gcd(b,a%b);
}
//整理成带分数形式
void print(long long a,long long b){
long long c=0;
if(a>0){ //正数
if(b==1){ //形如3/1
printf("%lld",a);
}
else if(a>b){ //形如5/3
c=a/b;
a-=b*c;
printf("%lld %lld/%lld",c,a,b);
}
else{ //真分数,形如3/5
printf("%lld/%lld",a,b);
}
}
else if(a==0){ //形如0/3
printf("%c",'0');
}
else{ //a<0
if(b==1){
printf("(%lld)",a); //形如-3/1
}
else if(-1*a>b){ //形如-5/3
c=a/b;
a=(-1*a)%b;
printf("(%lld %lld/%lld)",c,a,b);
}
else{
printf("(%lld/%lld)",a,b); //形如-1/3
}
}
}
void Addition(long long int a1,long long int b1,long long int a2,long long int b2){
print(a1,b1);
printf(" + ");
print(a2,b2);
printf(" = ");
long long a3=a1*b2+a2*b1;
long long b3=b1*b2;
//化简到最简形式,非假分数形式
long long gcd3=abs(gcd(a3,b3));
a3/=gcd3;
b3/=gcd3;
print(a3,b3);
printf("\n");
}
void Subtract(long long a1,long long b1,long long a2,long long b2){
print(a1,b1);
printf(" - ");
print(a2,b2);
printf(" = ");
long long a3=a1*b2-a2*b1;
long long b3=b1*b2;
//化简到最简形式,非假分数形式
long long gcd3=abs(gcd(a3,b3));
a3/=gcd3;
b3/=gcd3;
print(a3,b3);
printf("\n");
}
void Multiply(long long a1,long long b1,long long a2,long long b2){
print(a1,b1);
printf(" * ");
print(a2,b2);
printf(" = ");
long long a3=a1*a2;
long long b3=b1*b2;
//化简到最简形式,非假分数形式
long long gcd3=abs(gcd(a3,b3));
a3/=gcd3;
b3/=gcd3;
print(a3,b3);
printf("\n");
}
void Divide(long long a1,long long b1,long long a2,long long b2){
print(a1,b1);
printf(" / ");
print(a2,b2);
printf(" = ");
if(a2==0){
printf("Inf");
}
else if(a2<0){
long long a3=-1*a1*b2;
long long b3=-1*b1*a2;
long long gcd3=abs(gcd(a3,b3));
a3/=gcd3;
b3/=gcd3;
print(a3,b3);
}
else{ //化简到最简形式,非假分数形式
long long a3=a1*b2;
long long b3=b1*a2;
long long gcd3=abs(gcd(a3,b3));
a3/=gcd3;
b3/=gcd3;
print(a3,b3);
}
printf("\n");
}
int main(){
long long a1,b1,a2,b2;
scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
//先约分到最简形式
long long gcd1=abs(gcd(a1,b1)) ; //求最大公约数
a1/=gcd1;
b1/=gcd1;
long long gcd2=abs(gcd(a2,b2)) ; //求最大公约数
a2/=gcd2;
b2/=gcd2;
//统一用最简形式参与运算
Addition(a1,b1,a2,b2);
Subtract(a1,b1,a2,b2);
Multiply(a1,b1,a2,b2);
Divide(a1,b1,a2,b2);
return 0;
}
2、C++
#include
#include
using namespace std;
long long a, b, c, d;
long long gcd(long long t1, long long t2) {
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void func(long long m, long long n) {
int flag1 = 0, flag2 = 0, flag = 0;
if (n == 0) {
printf("Inf");
return ;
}
if (m == 0) {
printf("0");
return ;
}
if (m < 0) flag1 = 1;
if (n < 0) flag2 = 1;
m = abs(m), n = abs(n);
if (flag1 == 1 && flag2 == 1) flag = 0;
else if (flag1 == 1 || flag2 == 1) flag = 1;
if (m == n) {
if (flag == 1) printf("(-1)");
else printf("1");
return;
}
long long x = m % n, y = m / n;
if (x == 0) {
if (flag == 0) printf("%d", y);
else printf("(-%d)", y);
return ;
} else {
long long t1 = m - y * n, t2 = n, t = gcd(t1, t2);
t1 = t1 / t, t2 = t2 / t;
if (flag == 1) {
printf("(-");
if (y != 0) printf("%lld %lld/%lld)", y, t1, t2);
else printf("%d/%d)", t1, t2);
} else {
if (y != 0) printf("%lld %lld/%lld", y, t1, t2);
else printf("%lld/%lld", t1, t2);
}
}
}
void print() {
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c); printf("\n");
}
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
print();
return 0;
}