PAT 1088 Rational Arithmetic (20分)

原题链接:1088 Rational Arithmetic (20分)
关键词:数学、模拟
翻译: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

题目大意: 给出你两个分数,让你输出这两个数的加减乘除

分析: 这道题在2020.7.22模拟考遇到,但我却没做出来,想的太多了,全称用字符串处理……

题目主要实现一个函数func(m, n),他的作用是将一个分数m/n化简

  1. 注意使用long long,判断m、n是否异号不能直接乘,否则会溢出;
  2. 首先判断m、n中是否有0:m为0则结果为0,n为0则结果为Inf;
  3. 然后设置flag判断m、n是否异号,异号的话要输出-以及要多一个();
  4. 设置x为m除以n的结果,x若为0,则说明m/n是一个真分数,否则x作为带分数的整数部分输出,若是带分数,则m = m - n*x
  5. 最后还要对m/n进行约分,两个数都除以最大公约数;
  6. 最后用printf("%lld/%lld", m, n)的形式格式化输出m/n;

代码:

// pat 7-1
#include <bits/stdc++.h>
using namespace std;

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){	//对m/n进行化简  m分子 n分母 
	if(m * n == 0){
		printf("%s", n == 0 ? "Inf" : "0");
		return;
	}
	bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));	//flag判断m和n是否异号
	m = abs(m);
	n = abs(n);
	long long x = m / n;	//分数前面的整数部分
	if(flag) printf("%s", "(-"); 
	if(x != 0) printf("%lld", x);
	if(m % n == 0){	//能够整除 
		if(flag) printf(")");
		return ;	//只有整数部分,此时便可返回 
	}
	if(x != 0) printf(" ");
	m = m - x * n; 
	long long t = gcd(m, n);
	m = m / t;
	n = n / t;
	printf("%lld/%lld", m, n);
	if(flag) printf(")"); 
}

int main(){
	long long a, b, c, d;
	scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
	//printf("%lld %lld %lld %lld", a, b, c, d);
	
	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);	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值