PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, 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, difference, 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
就一句话,细节很重要!
#include <iostream>
using namespace std;
long long dv, res1, res2;
long long DIV(long long a, long long b)
{
if (b == )
return abs(a);
return DIV(b, a%b);
}
void print(long long a1, long long b1, long long a2, long long b2, char c)
{
if (a1 == )
printf("%d %c ", , c);
else
{
printf("%s", a1 > ? "" : "(");
dv = DIV(a1, b1);
a1 /= dv;
b1 /= dv; if (a1 / b1 != )
printf("%d", a1 / b1);
if (a1 - b1 * (a1 / b1) != )
printf("%s%d/%d", a1 / b1 != ? " " : "", a1 / b1 != ? abs(a1 - b1 * (a1 / b1)) : a1, b1);
printf("%s %c ", a1 > ? "" : ")", c);
} if(a2 == )
printf("%d %s ", , "=");
else
{
printf("%s", a2 > ? "" : "(");
dv = DIV(a2, b2);
a2 /= dv;
b2 /= dv;
if (a2 / b2 != )
printf("%d", a2 / b2);
if (a2 - b2 * (a2 / b2) != )
printf("%s%d/%d", a2 / b2 != ? " " : "", a2 / b2 != ? abs(a2 - b2 * (a2 / b2)) : a2, b2);
printf("%s %s ", a2 > ? "" : ")", "=");
} if (res1 == )
{
printf("%d\n",);
return;
}
else if (res2 == )
{
printf("Inf\n");
return;
}
printf("%s", res1 > ? "" : "(");
dv = DIV(res1, res2);
res1 /= dv;
res2 /= dv;
if (res1 / res2 != )
printf("%d", res1 / res2);
if (res1 - res2 * (res1 / res2) != )
printf("%s%d/%d", res1 / res2 != ? " " : "", res1 / res2 != ? abs(res1 - res2 * (res1 / res2)) : res1, res2);
printf("%s\n", res1 > ? "" : ")");
}
int main()
{
char c;
long long a1, b1, a2, b2;
cin >> a1 >> c >> b1 >> a2 >> c >> b2;
// +
res1 = a1 * b2 + a2 * b1;
res2 = b1 * b2;
print(a1, b1, a2, b2, '+');
// -
res1 = a1 * b2 - a2 * b1;
res2 = b1 * b2;
print(a1, b1, a2, b2, '-');
// *
res1 = a1 * a2;
res2 = b1 * b2;
print(a1, b1, a2, b2, '*');
// /
res1 = a2 > ? a1 * b2 : a1 * b2*-;
res2 = b1 * abs(a2);
print(a1, b1, a2, b2, '/');
return ;
}
PAT甲级——A1088 Rational Arithmetic的更多相关文章
- A1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题分类汇编——计算
本文为PAT甲级分类汇编系列文章. 计算类,指以数学运算为主或为背景的题. 题号 标题 分数 大意 1058 A+B in Hogwarts 20 特殊进制加法 1059 Prime Factors ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
随机推荐
- linux 服务器安装php5.6
查看原有的php版本:php -v 如果已经装了低版本的php,为了避免冲突,查看: yum list installed | grep php 删除:自行百度吧.可能不能一次性全部删除,只能一个一个 ...
- 数据库MySQL--连接查询
例子文件1:https://files.cnblogs.com/files/Vera-y/myemployees.zip 例子文件2:https://files-cdn.cnblogs.com/fil ...
- @Value的使用
<Spring源码解析>笔记 使用@Value赋值:1.基本数值2.可以写SpEL: #{}3.可以写${}:取出配置文件[properties]中的值(在运行环境变量里面的值) 1.创建 ...
- Java 基础 - 如何重写equals()
ref:https://www.cnblogs.com/TinyWalker/p/4834685.html -------------------- 编写equals方法的建议: 显示参数命名为oth ...
- .net下MVC中使用Tuple分页查询数据
主要是在DAL层写查询分页的代码. 例如DAL层上代码: public Tuple<List<WxBindDto>, int> GetMbersInfo(int start, ...
- Windows exit
退出 CMD.EXE 程序(命令解释器)或当前批处理脚本. EXIT [/B] [exitCode] /B 指定要退出当前批处理脚本而不是 CMD.EXE.如果从一个 ...
- servlet的抽取
servlet的抽取 servlet按照模块来划分,比如注册和登录的servlet就放到user的servlet中 原来: 登录时登录的servlet 注册时注册的servlet 现在: 登录注册的s ...
- 段错误 “段错误(segment fault)”、“非法操作,该内存地址不能read/write” 非法指针解引用造成的错误。
[root@test after_fc_distributed]# ./ffmpeg-linux64-v3.3.1 -i "concat:mymp3tmp/test_0.mp3|mymp3t ...
- 框架前期准备篇之AutoFac常见用法总结 转载
框架前期准备篇之AutoFac常见用法总结 一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势必会引起一点 ...
- 《DSP using MATLAB》Problem 8.33
代码: %% ------------------------------------------------------------------------ %% Output Info about ...