PAT_A1088#Rational Arithmetic
Source:
Description:
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 formk a/b
, wherek
is the integer part, anda/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, outputInf
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
Keys:
Code:
/*
Data: 2019-07-05 20:10:35
Problem: PAT_A1088#Rational Arithmetic
AC: 21:04 题目大意:
分数的加减乘除
*/
#include<cstdio>
#include<algorithm>
using namespace std;
struct fr
{
long long up,down;
}s1,s2; long long GCD(long long a, long long b)
{
return !b ? a : GCD(b,a%b);
} fr Reduct(fr r)
{
if(r.down < )
{
r.up = -r.up;
r.down = -r.down;
}
if(r.up==)
r.down=;
else
{
int gcd = GCD(abs(r.up),abs(r.down));
r.up /= gcd;
r.down /= gcd;
}
return r;
} fr Add(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down+f1.down*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Dif(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down-f1.down*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Pro(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Quo(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down;
r.down = f1.down*f2.up;
return Reduct(r);
} void Show(fr r)
{
r = Reduct(r);
if(r.up < )
printf("(");
if(r.down == )
printf("%lld", r.up);
else if(abs(r.up) > r.down)
printf("%lld %lld/%lld", r.up/r.down,abs(r.up)%r.down,r.down);
else
printf("%lld/%lld", r.up,r.down);
if(r.up < )
printf(")");
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%lld/%lld %lld/%lld", &s1.up,&s1.down,&s2.up,&s2.down);
char op[] = {'+','-','*','/'};
for(int i=; i<; i++)
{
Show(s1);printf(" %c ", op[i]);Show(s2);printf(" = ");
if(i==) Show(Add(s1,s2));
else if(i==) Show(Dif(s1,s2));
else if(i==) Show(Pro(s1,s2));
else
{
if(s2.up==) printf("Inf");
else Show(Quo(s1,s2));
}
printf("\n");
} return ;
}
PAT_A1088#Rational Arithmetic的更多相关文章
- PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- A1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
随机推荐
- dubbo系列学习好文章
1. dubbo入门学习(一)-----分布式基础理论.架构发展以及rpc.dubbo核心概念 https://www.cnblogs.com/alimayun/p/10982650.html 2. ...
- oracle服务端导出/导入方式expdp/impdp
1. expdp导出步骤 1.1 用sys管理员登录sqlplus [root@hxjk_test_mysql_redis_file oracle]# sqlplus SQL*Plus: Releas ...
- 中国HBase技术社区第一届Meetup资料大合集
2018年6月6号,由中国HBase技术社区组织,阿里云主办的中国第一次HBase Meetup在北京望京阿里中心举行,来自阿里.小米.滴滴.360等公司的各位HBase的PMC.committer共 ...
- js的内部特性--属性
使用方法:通过调用Object.defineProperty(对象,"对象属性",{}进行的操作}) 当对一个对象的属性的属性类型中vlue设置为一个值时,则这个对象的这个属性的值 ...
- 把数字翻译成字符串 --剑指offer 46题
# 给一个字符串,按如下规则把它翻译成字符串:1翻译成a,2翻译成b,...25翻译成z:一个数可以有多种翻译方式,比如122可以翻译成abb和kb还可以翻译成aw即3种翻译方式.计算一个数字有几种翻 ...
- Windows XP 下如何使用Qt Creator中的Git版本控制功能
原文地址:http://www.qtcn.org/bbs/simple/?t16960.html Qt Creator是针对Qt应用开发平台专门设计的IDE开发工具,集成了很多功能,分别有win ...
- HBase 热点问题——rowkey散列和预分区设计
热点发生在大量的client直接访问集群的一个或极少数个节点(访问可能是读,写或者其他操作).大量访问会使热点region所在的单个机器超出自身承受能力,引起性能下降甚至region不可用,这也会影响 ...
- 怎么在vue-cli中利用 :class去做一个底层背景或者文字的点击切换
// html <div class="pusherLists" :class="{hidden: isHidden}"> <span @cl ...
- .net微信扫码支付
今天给大家分享一篇.net的扫码支付文章,话不多说直接进入主题. 如有需要可以加我Q群[308742428]大家一起讨论技术,有偿服务. 后面会不定时为大家更新文章,敬请期待. 喜欢的朋友可以关注下. ...
- 数据结构:堆(Heap)
堆就是用数组实现的二叉树,所有它没有使用父指针或者子指针.堆根据"堆属性"来排序,"堆属性"决定了树中节点的位置. 堆的常用方法: 构建优先队列 支持堆排序 快 ...