Pat 甲级 训练真题集 1088!

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

细节决定成败。。。。

辗转相除求最大公约数

代码如下:

#include<iostream>
using namespace std;
long long a,b,c,d;

long long max_gcd(long long v1,long long v2){
	return v2==0?v1:max_gcd(v2,v1%v2);
}


void fun(long long v1,long long v2){
	int flag1=0,flag2=0;

	if(v2==0){
		printf("Inf");
		return;
	}
	if(v1==0){
		printf("0");
		return;
	}

	if(v1<0){
		v1=0-v1;
		flag1=1;
	}
	if(v2<0){
		v2=0-v2;
		flag2=1;
	}
	int flag=0;
	if(flag1==1&&flag2==1){
		flag=0;
	}else if(flag1==1||flag2==1){
		flag=1;
	}
	if(v1==v2){
		if(flag==1){
			printf("(-1)");
		}else{
			printf("1");
		}
		return;
	}
	long long v3=v1/v2;
	long long y=v1%v2;
	if(v1==0){
		if(flag==1){
			printf("(-%lld)",v3);
		}else{
			printf("%lld",v3);
		}
		return;
	}else{
		
		long long t=max_gcd(v1,v2);
		v1/=t;v2/=t;
		v1%=v2;
		if(flag==1){
			printf("(-");
			
			if(v1!=0&&v3!=0)printf("%lld %lld/%lld",v3,v1,v2);
			else if(v1!=0&&v3==0)printf("%lld/%lld",v1,v2);
			else if(v1==0&&v3!=0)printf("%lld",v3);
			else if(v1==0&&v3==0){
				printf("0");
			}
			printf(")");
		}else{
			if(v1!=0&&v3!=0)printf("%lld %lld/%lld",v3,v1,v2);
			else if(v1!=0&&v3==0)printf("%lld/%lld",v1,v2);
			else if(v1==0&&v3!=0)printf("%lld",v3);
			else if(v1==0&&v3==0){
				printf("0");
			}
		}	
		return;
	}
}

void sum(long long a,long long b,long long c,long long d){
	fun(a,b);
	printf(" + ");
	fun(c,d);
	printf(" = ");
	long long value1,value2,value3;
	value2=b*d;value1=a*d+c*b;
	value3=value1/value2;
	fun(value1,value2);
	printf("\n");
}
void diff(long long a,long long b,long long c,long long d){
	fun(a,b);
	printf(" - ");
	fun(c,d);
	printf(" = ");
	long long value1,value2,value3;
	value2=b*d;value1=a*d-c*b;	
	fun(value1,value2);
	printf("\n");
}
void pro(long long a,long long b,long long c,long long d){
	fun(a,b);
	printf(" * ");
	fun(c,d);
	printf(" = ");
	long long value1,value2,value3;
	value2=b*d;value1=a*c;	
	fun(value1,value2);
	printf("\n");
}
void quo(long long a,long long b,long long c,long long d){
	fun(a,b);
	printf(" / ");
	fun(c,d);
	printf(" = ");
	long long value1,value2,value3;
	value2=b*c;value1=a*d;	
	fun(value1,value2);
	printf("\n");
}
int main(){
	scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
	sum(a,b,c,d);
	diff(a,b,c,d);
	pro(a,b,c,d);
	quo(a,b,c,d);
	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦