题目

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. 020-PHP浏览目录

    <?php // 使用表格浏览目录的结构 print("<TABLE BORDER= '1'>"); // 创建表格的头 print("<TR&g ...

  2. 100-PHP二维数组的元素输出三

    <?php $stu=array(array(76,87,68), array(65,89,95), array(90,80,66), array(90,95,65)); //定义一个二维数组 ...

  3. php知识结构

    PHP的运行环境 连环境都搞不起来,就是你有多么喜欢PHP,那也是白搭,开始我们大多会使用集成环境软件例如xampp,wamp.随着知识的增加慢慢要学会自己搭建运行环境,例如 Linux(Ubuntu ...

  4. iOS 技术篇:从使用到了解block底层原理 (二)

    block实质 序言 上篇文章中主要通过简单的demo展示了block的使用场景,本篇将基于上篇文章iOS 技术篇:从使用到了解block底层原理 (一)进一步了解block底层的实现原理. bloc ...

  5. String的Split使用方法(以特定字符分隔,提取所需信息)

    此处复制一串以空格分隔的数字,提取数字进行排序 int[] a = new int[10]; string input = Console.ReadLine();//获取用户输入的字符串 char[] ...

  6. 在MFC做DLL动态链接库时,使用boost,出现断言错误

    建立的MFC DLL工程中有使用boost::thread,就会发生compile正常但是一程式执行或者直接编辑就出現ASSERT错误. 错误位置:dllinit.cpp,Line: 587,ASSE ...

  7. Tomcat9卸载与安装

    Tomcat9卸载与安装 首先确定环境变量配置正确,按实际的安装路径来设置. 在tomcat9的bin目录下打开命令行窗口 按住shift键不放,点击右键 输入以下命令 在打开的命令行窗口中输入以下命 ...

  8. C++的模板类:不能将定义与声明写在不同文件中

    问题来源 今天看了orbslam2自带的第三方库DBoW2的TemplatedVocabulary.h文件,发现其中模板类的函数成员的定义与声明放在了同一个文件:同时发现,DBoW2的CMakeLis ...

  9. 使用技巧 --- 与VS Code相关

    目的:修改VS Code的注释文本颜色 S1:假设VS Code的安装路径是 %MVSC% S2:文件资源管理器进入目录 %MVSC%\resources\app\extensions\ S3:该目录 ...

  10. SpringMVC使用可以访问静态资源,但是导致Controller访问失败

    如果在web.xml 拦截配置如下: <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC&l ...