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 ...
随机推荐
- songtzu的创业产品的经历
我的产品是关于卡通头像的东东,也有点照片处理app的感觉.你可能会想到脸萌.或者足迹.可是.我自觉得,我比这两者想做的东西要好. 以下的两张是站点首页的效果图. 图片版权与肖像权全部,非授权不得使用. ...
- 4、angularJS过滤器
一.过滤器的作用 过滤器用来格式化须要展示给用户的数据. 在HTML中的模板绑定符号{{ }}内通过|符号来调用过滤器. 比如.如果我们希望将字符串转换成大写能够对字符串中的每一个字符都单独进行转换操 ...
- 查看scn headroom变化趋势的几种方法
查看scn headroom变化趋势的几种方法 scn headroom问题,本文不做解释. 本文为自己的总结,脚本来自于oracle sr技术project师. 转载请注明出处http://blog ...
- SpringMVC + MyBatis 配置文件
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...
- java web支持jsonp跨域
jsonp跨域请求处理 Jsonp(JSON with Padding) 是 json的一种"使用模式",可以让网页从别的域名(网站)那获取资料,绕过同源策略(若地址里面的协议.域 ...
- Oracle调整内存参后报ORA-00844和ORA-00851
数据库服务器内存由16G增加为64G,为充分利用内存资源,对Oracle内存参数做了如下调整: SQL>alter system set sga_max_size=40960M scope=sp ...
- Asp.net MVC Checkbox控件 和 Nullable<bool>, 或bool?类型
@Html.CheckBoxFor() 这个方法生成两个Input HTML标签,不明白为什么这样,如果数据库是Nullable<bool>类型,就会报错. 网上的解决方法是这样: 方法一 ...
- input表单(02)
01.表单的代码实现 <!DOCTYPE html> <html> <head> <title>世纪佳缘,你在我也在</title> < ...
- JavaScript Array 整理 - 元素操作
整理一下Array对象中针对元素操作的方法. 分别是: concat (组合数组) join(数组转字符串) pop(删除最后一个元素) shift(删除第一个元素) push(在数组尾部添加新元素) ...
- 【SQL】MERGE
MERGE可以合并多个表中的数据,也可实现多表中数据的同步.使用MERGE语句对表中数据进行有条件的更新和插入.当查找的行存在时,UPDATE更新行中的数据:当查找的行不存在时,INSERT插入数据. ...