PAT 1088. 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>
#include<math.h>
using namespace std;
long long int getgcd(long long int s,long long int m){
int r;
while(r=s%m){
s=m;
m=r;
}
return m;
}
void print(long long int s,long long int m){
int sign=1;
if(m==0){
cout<<"Inf";
return;
}
if(s<0){
s=abs(s);
sign*=-1;
}
if(m<0){
m=abs(m);
sign*=-1;
}
int gcd=getgcd(s,m);
s/=gcd;
m/=gcd;
if(sign<0) cout<<"(-";
if(m==1) cout<<s;
else if(s>m) cout<<s/m<<" "<<s%m<<"/"<<m;
else cout<<s<<"/"<<m;
if(sign<0) cout<<")";
}
int main(){
long long int s1,m1,s2,m2;
char op[4]={'+','-','*','/'};
scanf("%lld/%lld %lld/%lld",&s1,&m1,&s2,&m2);
for(int i=0;i<4;i++){
print(s1,m1);
cout<<" "<<op[i]<<" ";
print(s2,m2);
cout<<" = ";
switch(i){
case 0:print(s1*m2+s2*m1,m1*m2); break;
case 1:print(s1*m2-s2*m1,m1*m2); break;
case 2:print(s1*s2,m1*m2); break;
case 3:print(s1*m2,m1*s2); break;
}
cout<<endl;
}
return 0;
}
PAT 1088. Rational Arithmetic的更多相关文章
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- 【PAT甲级】1088 Rational Arithmetic (20 分)
题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- 1088 Rational Arithmetic
题意: 给出两个分式(a1/b1 a2/b2),分子.分母的范围为int型,且确保分母不为0.计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出 ...
- 1088. Rational Arithmetic (20)
1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...
- PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
随机推荐
- mongodb数据库的启动和停止
数据库的启动和停止是数据库最主要的操作,也是数据库可以提供服务和被连接管理的前提条件.不同的数据库启动和停止的方式有一些差异.但也有同样之处,启动和关闭也必然会和数据库的进程有关 ...
- GDI+学习之------色彩与图像
色彩 在GDI+中.色彩是通过Color类来描写叙述的.不是用RGB类.用RGB构造会出错.GDI+中的色彩信息值是由一个32位的数据来表示的,它包含8位alpha值和各8位的R.G.B值,对于alp ...
- luogu2763 试题库问题 二分匹配
关键词:二分匹配 本题用有上下界的网络流可以解决,但编程复杂度有些高. 每个类需要多少题,就设置多少个类节点.每个题节点向其所属的每一个类节点连一条边.这样就转化成了二分匹配问题. #include ...
- golang LMDB入门例子——尼玛,LMDB的文档真的是太少了
使用的是这个库:https://github.com/szferi/gomdb 安装: go get github.com/szferi/gomdb 代码: package main import ( ...
- 【POJ 3764】 The xor-longest path
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- 第3课 把文件存入Git文档库
3-1 排除不需要加入文档库的文件 Git追踪文件的方式.Git会将文件和文件夹分成以下三类: 1. 被追踪的(tracked): 2. 忽略的(ignored): 3. 不被追踪的(u ...
- PCB MS SQL 将字符串分割,并指定索引返回字符串(标量函数)
Create FUNCTION [dbo].[SplitIndex] ( @str AS VARCHAR(max), @Index AS INT, ) = '/' ) ) AS BEGIN ) --待 ...
- Balloons(DFS)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2248 题意:(1)求图中四连块(有公共边的方块 ...
- codevs2594解药还是毒药(状压dp)
2594 解药还是毒药 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Smart研制出对付各种症状的解药,可是 ...
- [SDOI2004]打鼹鼠
...... 心血来潮,手打abs 结果...BZOJ上CE,洛谷上WA... 把宏定义换成函数就过了 显然一个点可以走到另一个点,当且仅当两点鼹鼠出现时间$\leq$两点间距离的曼哈顿距离 显然是D ...