PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, diference, 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, diference, 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
题目分析
已知两个有理数,进行加减乘除操作
解题思路
- 求最大公约数的函数,方便化简
- 分数化简函数
2.1 分子为0,分母置为1
2.2 分子与分母最大公约数化简
2.3 分母为负,分子分母同时取反 - 分数打印
3.1 假分数转换为整数部分+真分数部分再打印
Code
Code 01
#include <iostream>
using namespace std;
// 求最大公约数
int gcd(long long a, long long b) {
return b==0?abs(a):gcd(b,abs(a)%b);
}
// 化简
void reduction(long long &a,long long &b) {
if(b<0) { //若b小于0,分子分母同时取反
a=-a;
b=-b;
}
if(a==0) { // 若a为0,将分母置为1(同分子能被分母整除的情况统一处理)
b=1;
} else { // 分子分母公因数化简
long long gcdv = gcd(a,b);
a/=gcdv;
b/=gcdv;
}
}
// 打印分式
void showi(long long a, long long b) {
if(b==0||a==0) {
printf("%s",a==0?"0":"Inf");
return ;
}
long long tempa = a,tempb = b;
reduction(tempa,tempb);
if(tempa<0)printf("(");
if(tempb==1)printf("%lld",tempa);
else if(abs(tempa)>abs(tempb)) {
printf("%lld %lld/%lld",tempa/tempb,abs(tempa%tempb),tempb);
} else {
if(tempa!=0)printf("%lld/%lld",tempa,tempb);
}
if(tempa<0)printf(")");
}
void add(long long a1, long long b1, long long a2, long b2) {
showi(a1,b1);
printf(" + ");
showi(a2,b2);
printf(" = ");
showi(a1*b2+a2*b1,b1*b2);
printf("\n");
}
void sub(long long a1, long long b1, long long a2, long b2) {
showi(a1,b1);
printf(" - ");
showi(a2,b2);
printf(" = ");
showi(a1*b2-a2*b1,b1*b2);
printf("\n");
}
void mul(long long a1, long long b1, long long a2, long b2) {
showi(a1,b1);
printf(" * ");
showi(a2,b2);
printf(" = ");
showi(a1*a2,b1*b2);
printf("\n");
}
void div(long long a1, long long b1, long long a2, long b2) {
showi(a1,b1);
printf(" / ");
showi(a2,b2);
printf(" = ");
showi(a1*b2,a2*b1);
printf("\n");
}
int main(int argc,char *argv[]) {
long long a1,b1,a2,b2,gcdv;
scanf("%lld/%lld %lld/%lld", &a1,&b1,&a2,&b2);
add(a1,b1,a2,b2);
sub(a1,b1,a2,b2);
mul(a1,b1,a2,b2);
div(a1,b1,a2,b2);
return 0;
}
PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]的更多相关文章
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- 【PAT甲级】1088 Rational Arithmetic (20 分)
题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...
- PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]
题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...
- PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...
- 1088. Rational Arithmetic (20)
1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...
- PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]
题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
随机推荐
- Mac电脑上怎么设置环境变量
https://jingyan.baidu.com/article/8065f87f47b29523312498e4.html 环境变量是电脑操作系统中常用的一些变量,作用类似于将一些常用命令所在的文 ...
- 联系我们地图坐标展示js
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6d88 ...
- vzray服务端配置
打开securecrt登陆服务器 输入命令:bash <(curl -s -L https://git.io/vzray.sh)1 回车tcp 回车端口 回车默认 回车
- Django配置日志
在settings里配置 # 日志配置 LOGGING = { # 是python的版本 'version': 1, # 是否禁用 'disable_existing_loggers': False, ...
- 【LeetCode】 240. 搜索二维矩阵 II
题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 mat ...
- TypeScript 文件引入 Html (ts import html webpack)
我们的目标是把html引入ts文件,webpack打包时就能把html打进js文件,减少文件加载啦 1 安装 text-loader npm install text-loader --save-de ...
- 19 01 16 jquery 的 属性操作 循环 jquery 事件 和事件的绑定 解绑
jquery属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').htm ...
- ASP.NET Identity实现分布式Session,Docker+Nginx+Redis+ASP.NET CORE Identity
零.背景介绍 在学习ASP.NET CORE开发的过程中,身份认证是必须考虑的一项必要的组件.ASP.NET CORE Identity是由微软官方开发的一整套身份认证组件,兼具完整性和自由度.Doc ...
- Spring Boot2(001):入门介绍和一些官网链接参考
Spring官方文档比较齐全,学习的过程中可以多参考官方文档,最权威的版本.01.Spring Boot的一些官方链接 01.01 Spring Boot官网 https://spring.io/pr ...
- 不同DIV滚动条如何同步?
$(".DIV").scroll(function(){ $(".target").scrollLeft($(".DIV ").scrol ...