PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20)
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
- 思路
- 分数的四则运算,难点主要是在输出数据的格式化(参见代码stringFormat函数)上,比较考验耐心和逻辑,仔细就行。
- 代码
- #include<iostream>
- using namespace std;
- typedef long long ll;
- ll a,b,c,d,gcdvalue;
- ll gcd(ll a,ll b)
- {
- return b == 0?abs(a):gcd(b,a % b);
- }
- string stringFormat(ll a,ll b)
- {
- string res = "";
- gcdvalue = gcd(a,b);
- a /= gcdvalue;
- b /= gcdvalue;
- if(a * b < 0)
- res += "(-";
- ll c = abs(a), d = abs(b);
- if(c/d == 0)
- {
- if( c % d == 0)
- res += "0";
- else
- res += to_string(c) + "/" +to_string(d);
- }
- else
- {
- res += to_string(c/d);
- if(c % d != 0)
- {
- res += " " + to_string(c % d) + "/" +to_string(d);
- }
- }
- if(a * b < 0)
- res += ")";
- return res;
- }
- int main()
- {
- scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
- ll sum1,sum2,sub1,sub2,mul1,mul2,div1,div2;
- //sum
- sum1 = a * d + b * c;
- sum2 = b * d;
- sub1 = a * d - b * c;
- sub2 = b * d;
- mul1 = a * c;
- mul2 = b * d;
- div1 = a * d;
- div2 = b * c;
- string a1 = stringFormat(a,b);
- string a2 = stringFormat(c,d);
- cout << a1 << " + " << a2 <<" = " << stringFormat(sum1,sum2) << endl;
- cout << a1 << " - " << a2 <<" = " << stringFormat(sub1,sub2) << endl;
- cout << a1 << " * " << a2 <<" = " << stringFormat(mul1,mul2) << endl;
- if(a2 != "0")
- cout << a1 << " / " << a2 <<" = " << stringFormat(div1,div2) << endl;
- else
- cout << a1 << " / " << a2 <<" = " << "Inf" << endl;
- }
PAT1088:Rational Arithmetic的更多相关文章
- pat1088. Rational Arithmetic (20)
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 ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- 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 ...
随机推荐
- mysql进阶(八)怎么对varchar类型排序问题
MySQL中怎么对varchar类型排序问题 asc 升级 desc降序 在mysql默认order by 只对数字与日期类型可以排序,但对于varchar字符型类型排序好像没有用了,下面我来给各位同 ...
- 菜鸟玩云计算之廿二: saltstack 配置
菜鸟玩云计算之廿二: saltstack 配置 要求环境: RHEL6.4+ >=Python2.6.6, < Python 3.0 关闭salt-master/minion服务: # ...
- STL - miltimap(可重映射)
#include <iostream> #include <map> #include <string> using namespace std; //Multim ...
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes ...
- ERP-非财务人员的财务培训教(二)------如何评价公司/部门经营业绩
一.财务比率分析 第一节 流动性比率 第二节 经营比率 第三节 资本结构比率 第四节 获利能力比率 第五节 现金流量比率 第六节 获现能力比率 二.财务比率金字塔 第二部分 如何评价公 ...
- spring注解关键字
spring注解: (1)@Controller 控制器 (2)@Autowired 按照类型匹配,可以完成对类成员变量,方法及构造函数进行标注,完成自动装配的工作 @Autowired ...
- 解决os x下gdb不能调试的问题
在较新的os x中使用gdb调试程序会发生 Unable to find Mach task port 之类的错误,这是由于新的os x内核禁止未授权的程序随意控制其他进程,如果不用root权限的话, ...
- Salesforce Lightning开发学习(一)Hello World开发实践
一:什么是Lightning Component framework Lightning Component framework 简称Lightning,是Salesforce封装的一个前端框架,开发 ...
- Mego(04) - NET简单实现EXCEL导入导出
前言 相信做过信息系统的朋友都会遇到EXCEL导入导出的相关开发,做过不少EXCEL导入导出后总结起来大致有如下几种方式实现: ADO.NET的OldDb或ODBC连接EXCEL使用DataTable ...
- JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性)
JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性) 以下内容: 一.获取元素的属性 二.设置元素的属性 三.删除元素的属性 一.获取元素的属性 1-原生JS 获取属性 .ge ...