题目

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

题目分析

已知两个有理数,进行加减乘除操作

解题思路

  1. 求最大公约数的函数,方便化简
  2. 分数化简函数

    2.1 分子为0,分母置为1

    2.2 分子与分母最大公约数化简

    2.3 分母为负,分子分母同时取反
  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) [数学问题-分数的四则运算]的更多相关文章

  1. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  2. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  3. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  4. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  5. 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 ...

  6. PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]

    题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...

  7. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  8. PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]

    题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...

  9. pat1088. Rational Arithmetic (20)

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

随机推荐

  1. Web前端开发CSS规范总结

    作为Web前端开发必备语言,CSS为大家广为熟知,今天就跟大家分享下CSS规范总结,Web前端的小伙伴们看过来吧! CSS样式的权值(权重) 权值等级的定义 第一等:代表内联样式,如: style=” ...

  2. C++ 99表

    #include<iostream> using namespace std; class Sumes { public: int sum; int i, j; }; int main() ...

  3. sendgrid 批量发送邮件,收件栏只显示当前用户的方案

    需求:批量发送邮件,用户可能看到其他用户的邮箱地址,之前用BBC发送,但问题是接收地址是同一个. 官方解决方案:https://sendgrid.kke.co.jp/docs/Tutorials/A_ ...

  4. 20180122 PyTorch学习资料汇总

    PyTorch发布一年团队总结:https://zhuanlan.zhihu.com/p/33131356?gw=1&utm_source=qq&utm_medium=social 官 ...

  5. 大数据高可用集群环境安装与配置(09)——安装Spark高可用集群

    1. 获取spark下载链接 登录官网:http://spark.apache.org/downloads.html 选择要下载的版本 2. 执行命令下载并安装 cd /usr/local/src/ ...

  6. Linux command line and shell scripting buble

    Chapter 4 More bash shell Commands 1. ps ps -ef 2. top 3. kill 3940 kill -s HUP 3940 killall http* 4 ...

  7. python刷LeetCode:20. 有效的括号

    难度等级:简单 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序 ...

  8. 好文推荐:终于有人把Elasticsearch原理讲透了

    专注于Java领域优质技术,欢迎关注 作者:channingbreeze 转自公号:互联网侦察 小史是一个非科班的程序员,虽然学的是电子专业,但是通过自己的努力成功通过了面试,现在要开始迎接新生活了. ...

  9. 配置自己的sublime

    我配置的sublime的是这样的,就是在input里输入数据,然后在output里可以得到数据,这样比较方便,看到有的大神还配置背景和其他的,有时间搞一下: 首先把编译器g++配置到环境变量,可以从d ...

  10. [GXYCTF2019]Ping Ping Ping

    0x00 知识点 命令执行变量拼接 /?ip=127.0.0.1;a=g;cat$IFS$1fla$a.php 过滤bash用sh执行 echo$IFS$1Y2F0IGZsYWcucGhw|base6 ...