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 ...
随机推荐
- luogu1064 金明的预算方案
这道题我就想说一点:审题!附件只有2个!钱是10的整数倍,不是100的整数倍! #include <cstdio> #include <cstring> #include &l ...
- bzoj 4025 二分图 分治+并查集/LCT
bzoj 4025 二分图 [题目大意] 有n个点m条边,边会在start时刻出现在end时刻消失,求对于每一段时间,该图是不是一个二分图. 判断二分图的一个简单的方法:是否存在奇环 若存在奇环,就不 ...
- bzoj1725 [Usaco2006 Nov]Corn Fields牧场的安排(状压dp)
1725: [Usaco2006 Nov]Corn Fields牧场的安排 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 714 Solved: 502 ...
- [Apple开发者帐户帮助]三、创建证书(3)创建企业分发证书
作为Apple Developer Enterprise Program的成员,您可以创建多个企业分发证书. 所需角色:帐户持有人或管理员. 在证书,标识符和配置文件中,从左侧的弹出菜单中选择iOS, ...
- 利用poi,jxl将Excel数据导入数据库
需求:‘需要将本地的Excel中的数据经过验证之后导入数据库,在导入数据库之前在页面上展示出来 思路:将Excel导入存到session里面 去判断有没有不合法数据 如果有阻止提交 工具类: imp ...
- 使用idea2.5建立maven项目
使用idea的步骤: 1.建立一个新的maven项目 2.选中maven项目 3.点击next,输入groupID和artifactid 4.点击next 选择projectlocation 5.选 ...
- c#,Java aes加密
1.c#版本 /// <summary> /// Aes加密解密.c#版 /// </summary> public class BjfxEncryptHelper { /// ...
- Win10,JDK8,tomact7.0.85配置
今天在win10上配置了jdk以及tomact先前使用jdk7+tomact7.0.56运行失败. 后经调整后正确配置注意事项如下: 1.下载并按照jdk-8u161-windows-x64,默认安装 ...
- ACM_写数字
写数字 Time Limit: 2000/1000ms (Java/Others) Problem Description: 把由1开始的自然数依次写下来:123456789101112……,重新分组 ...
- 页面中word文本框的编辑,两种方式
大致效果图(对其中的功能可以增减): 实现方法1:调用js <link href="../../platform/js/kindeditor/themes/default/defaul ...