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 ...
随机推荐
- Struts2—Action
二.命名空间namespace ·命名空间namespace须要放在相应的package下 ·Namespace必须以"/开头". ·Result的name是"succe ...
- python 005 正则表达式
. 任意字符 ^ 匹配字符串开始 $ 匹配字符串结尾 * 匹配前面出现了零次或多次 + 匹配前面出现了一次或多次 ? 匹配前面出现零次或一次 {N} 匹配前面出现了N次 {M,N} 匹配重复出现M-N ...
- 【Android开发VR实战】三.开发一个寻宝类VR游戏TreasureHunt
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53939303 本文出自[DylanAndroid的博客] [Android开发 ...
- 封装RecyclerViewAdapter实现RecyclerView下拉刷新上拉载入很多其它
实现 关于下拉刷新使用的是github上的项目Ultra Pull To Refresh项目. gradle依赖例如以下: compile 'in.srain.cube:ultra-ptr:1.0.1 ...
- mongoDB学习笔记——存取图片(C#)
作为一个NoSql数据库的代表,存取多媒体数据,应该是强项吧?那么,图片在mongoDB里是如何存取的呢?(其实,关系型数据库存取图片也一点问题没有,所以我看NoSql的强项不在于是否存储多媒体,而在 ...
- 题解 UVA10587 【Mayor's posters】
先讲一下:dalao @lisuier 发布的前一篇题解严格来讲是有错误的 比如下一组数据: 1 3 1 10 1 4 7 10 显然答案是3,然而用lisuier dalao的程序做出来的答案是2( ...
- 跳出双重for循环的案例__________跳出当前循环(continue out)
package com.etc.operator; public class demo { public static void main(String[] args) { // break out; ...
- [你必须知道的.NET]目录导航
http://www.cnblogs.com/anytao/archive/2007/09/14/must_net_catalog.html
- WebApi里面路由机制的原理以及路由匹配的过程
1.WebApi服务启动之后,会执行全局配置文件Global.asax.cs的 protected void Application_Start(){GlobalConfiguration.Confi ...
- Spring Boot (2) Restful风格接口
Rest接口 动态页面jsp早已过时,现在流行的是vuejs.angularjs.react等前端框架 调用 rest接口(json格式),如果是单台服务器,用动态还是静态页面可能没什么大区别,如果服 ...