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 ...
随机推荐
- C++ do while无限循环~
#include<iostream> using namespace std; #include<Windows.h> int main() { ; ; system(&quo ...
- Elasticsearch 集群 - 健康检查
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- PGSQL基本操作语句
; --更新数据 ,,) ; --插入数据 ORDER BY app_name,flag asc/desc ; --查询数据并且排序 offset ; --查询起点0开始查询,返回5条数据 ORDER ...
- Django 异常处理
我们新建一个py文件 # 在restful中导入exception_handler from rest_framework.views import exception_handler from dj ...
- OpenCV3 Ref SVM : cv::ml::SVM Class Reference
OpenCV3 Ref SVM : cv::ml::SVM Class Reference OpenCV2: #include <opencv2/core/core.hpp>#inclu ...
- 【pwnable.kr】leg
pwnable从入门到放弃第八题. Download : http://pwnable.kr/bin/leg.cDownload : http://pwnable.kr/bin/leg.asm ssh ...
- 66.Python中startswith和endswith的使用
定义模型的models.py,示例代码如下: from django.db import models class Category(models.Model): name = models.Char ...
- bfs--P1301 魔鬼之城
*传送 求最小步数,bfs求解.因为题目要求可以走八个方向(上下左右和对角线),所以两个方位数组来找八个方向 int dirx[9]={0,0,1,1,1,0,-1,-1,-1}; int diry[ ...
- Web前端工程师需要注意的开发规范有哪些?
从事web前端开发工作我们就需要了解web前端开发的规范,这样才能保证高效快速的完成工作,本篇就和大家分享一下web前端开发工程师需要注意的web前端开发规范有哪些,希望对小伙伴们有所帮助. web前 ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...