题目

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. 105-PHP使用var_dump查看类的类型

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } $ren1=new ren(); //实例化人类 $mao1=new mao(); //实例化猫类 $ ...

  2. jQuery原理系列-Dom Ready

    ready事件是jquery的一个很重要的功能,在很久很久以前,我们是使用window.onload监听页面加载成功的,onload事件的好处是你不用考虑浏览器兼容性,也不需要依赖任何框架就可以写,但 ...

  3. JAVA - SpringBoot项目引用generator生成 Mybatis文件

    JAVA - SpringBoot项目引用generator生成 Mybatis文件  在spring官网https://start.spring.io/自动生成springboot项目,这里选择项目 ...

  4. 导出execl

    string filepath = Utils.GetMapPath("/upload/excel/"); filepath = filepath + fileName + &qu ...

  5. vue学习(十二)vue全家桶 Vue-router&Vuex

    一 vue-router的安装 二 vue-router的基本使用 三 命名路由 四 动态路由的匹配和路由组件的复用 一 vue-router的安装 NPM npm install vue-route ...

  6. 【转载】WebDriver拾级而上·之零 WebDriver理论

    Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)   Selenium1.0可以使用任何编程语言,但是有个先决条件就是必须支 ...

  7. js 琐碎

    1.setTimeout() .setInterval() setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式.(即n毫秒后执行一次) setTimeout(code,n) set ...

  8. 【pwnable.kr】fb

    这是pwnable.kr的签到题,记录pwn入门到放弃的第一篇. ssh fd@pwnable.kr -p2222 (pw:guest) 题目很简单,登录上了ssh后,发现了3个文件:fd,fd.c, ...

  9. 66.Python中startswith和endswith的使用

    定义模型的models.py,示例代码如下: from django.db import models class Category(models.Model): name = models.Char ...

  10. 一本通1166 求f(x,n)

    [题目描述] 已知 计算x=4.2,n=1以及x=2.5,n=15时f的值. [输入] 输入x和n. [输出] 函数值,保留两位小数. [输入样例] 4.2 10 [输出样例] 3.68 1.看见这种 ...