PAT-A-1088 Rational Arithmetic 【最小公倍数】 【最大公约数】

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

Tips:(1)虽然题目中保证输出和输出的数字都在long int范围内,但是我们在运算过程中,分母通分,导致两个int类型数字相乘是有可能超出int,所以要使用long long 类型.这个我必须得服,必须引以为戒。

多说几句,因为这道题比较经典

  • 约分:大小判断->交换;小于零的变成相反数;
  • 输出分数,因为设计到相乘的操作,分母可能为负数,(相加减操作不会),如果分母是负数,则分母取反,分子取反
  • 输出方式:按照分数的可能出现的三种方式进行输出即整数、真分数、假分数
#include <iostream>
using namespace std;
long long getYue(long long a,long long b){      //类型long long
	if(a<0) a=-a;
	if(b<0) b=-b;                               //负数取反
	long long temp;
	if(a<b){
		temp=a;                                 //颠倒顺序
		a=b;
		b=temp;
	}
	while(b!=0){                                //辗转相除
		temp=a%b;
		a=b;
		b=temp;
	}
	return a;
}
void print(long long fenzi,long long fenmu){ 
	if(fenmu<0){
		fenmu=-fenmu;                          //分母为负,分母取反,分子取反
		fenzi=-fenzi;
	}
	long long yue=getYue(fenzi,fenmu);        //约分
	fenzi/=yue;
	fenmu/=yue;                               
	bool neg=false;
	if(fenzi<0) neg=true;                     //负数标志
	if(neg){
		printf("(-");
		fenzi=-fenzi;
	}
	if(fenmu==1) printf("%lld",fenzi);  //整数
	else if(fenzi<fenmu) printf("%lld/%lld",fenzi,fenmu); //真分数
	else printf("%lld %lld/%lld",fenzi/fenmu,fenzi%fenmu,fenmu);  //假分数 
	if(neg) printf(")");
}
int main(){
	long long fenzi,tfenzi,fenmu,tfenmu,a,b;
	scanf("%lld/%lld %lld/%lld",&fenzi,&fenmu,&tfenzi,&tfenmu);
	//加法
	print(fenzi,fenmu);
	printf(" + ");
	print(tfenzi,tfenmu);
	printf(" = ");
	a=fenzi*tfenmu+tfenzi*fenmu;
	b=fenmu*tfenmu;
	print(a,b);
	printf("\n");
	//减法
	print(fenzi,fenmu);
	printf(" - ");
	print(tfenzi,tfenmu);
	printf(" = ");
	a=fenzi*tfenmu-tfenzi*fenmu;
	b=fenmu*tfenmu;
	print(a,b);
	printf("\n");
	//乘法
	print(fenzi,fenmu);
	printf(" * ");
	print(tfenzi,tfenmu);
	printf(" = ");
	a=fenzi*tfenzi;
	b=fenmu*tfenmu;
	print(a,b);
	printf("\n");
	//除法
	print(fenzi,fenmu);
	printf(" / ");
	print(tfenzi,tfenmu);
	printf(" = ");
    if(tfenzi==0||tfenzi==-0){                   //题目要求说除法运算,结果的分母为0输出Inf
        printf("Inf\n");                         //而题目中保证输入的分数分母都不等于0
        return 0;                                //那就只有一种情况,后面的分子绝对值等于0
    }
	a=fenzi*tfenmu;
	b=fenmu*tfenzi;
	print(a,b);
	printf("\n");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值