PAT 1088. Rational Arithmetic (20)(分数加减乘除)(待修改)
来源:互联网 发布:mac redis 编辑:程序博客网 时间:2024/05/28 15:43
官网
1088. Rational Arithmetic (20)
时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
解题思路
注意 0/5,-8/2 ,5/0,-7/5,-14/10,-5/7等输入。
待修改代码
#include<iostream>#include<cstdio>#include<math.h>#include<algorithm>using namespace std;void p(long long z, long long m){ bool flag = false; if (z*m < 0){ flag = true; cout << "(-"; z = abs(z); m = abs(m); } if (z%m==0) { cout << z / m; } else{ int pos = 0; if (z>m) { pos = z / m; cout << pos; z = z%m; } if (pos>0) { if (z>0) { cout << " "; int l = 1; for (long long i = 2; i <= z; i++) { if (z%i == 0 && m%i == 0) { l = i; } } z = z / l; m = m / l; cout << z << "/" << m; } } else{ if (z>0) { int l = 1; for (long long i = 2; i <= z; i++) { if (z%i == 0 && m%i == 0) { l = i; } } z = z / l; m = m / l; cout << z << "/" << m; } else if (z == 0) { cout << 0; } } } if (flag) { cout << ")"; }}void add(int fz1, int fm1, int fz2, int fm2){ long long temz = fz1*fm2 + fz2*fm1; long long temm = fm1*fm2; p(fz1, fm1); cout << " + "; p(fz2, fm2); cout << " = "; p(temz, temm); cout << endl;}void difference(int fz1, int fm1, int fz2, int fm2){ long long temz = fz1*fm2 - fz2*fm1; long long temm = fm1*fm2; p(fz1, fm1); cout << " - "; p(fz2, fm2); cout << " = "; p(temz, temm); cout << endl;}void product(int fz1, int fm1, int fz2, int fm2){ long long temz = fz1*fz2; long long temm = fm1*fm2; p(fz1, fm1); cout << " * "; p(fz2, fm2); cout << " = "; p(temz, temm); cout << endl;}void quotient(int fz1, int fm1, int fz2, int fm2){ long long temz = fz1*fm2; long long temm = fm1*fz2; if (temm == 0) { p(fz1, fm1); cout << " / "; p(fz2, fm2); cout << " = Inf" << endl; } else{ p(fz1, fm1); cout << " / "; p(fz2, fm2); cout << " = "; p(temz, temm); cout << endl; }}int main(){ int fz1, fm1, fz2, fm2; scanf("%d/%d %d/%d", &fz1, &fm1, &fz2, &fm2); //消除公共因子 int k = 1; for (int i = 2; i <= min(fz1, fm1); i++) { if (fz1%i == 0 && fm1%i == 0) { k = i; } } fz1 = fz1 / k; fm1 = fm1 / k; k = 1; for (int i = 2; i <= min(fz2, fm2); i++) { if (fz2%i == 0 && fm2%i == 0) { k = i; } } fz2 = fz2 / k; fm2 = fm2 / k; //输出 add(fz1, fm1, fz2, fm2); difference(fz1, fm1, fz2, fm2); product(fz1, fm1, fz2, fm2); quotient(fz1, fm1, fz2, fm2); return 0;}
- PAT 1088. Rational Arithmetic (20)(分数加减乘除)(待修改)
- PAT 1081. Rational Sum (20)(分数加法)(待修改)
- PAT (Advanced Level) 1088. Rational Arithmetic (20) 分数四则运算
- PAT 1088. Rational Arithmetic (20) 分数模拟计算+字符串处理
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 【PAT】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic
- 【PAT】1088. Rational Arithmetic
- PAT Rational Arithmetic (20)
- 分数计算--PAT.A1081. Rational Arithmetic
- PAT A 1088. Rational Arithmetic (20)
- PAT-A 1088. Rational Arithmetic (20)
- PAT-A-1088. Rational Arithmetic (20)
- Pat(A) 1088. Rational Arithmetic (20)
- 【PAT】【Advanced Level】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20) 模块化
- 截取指定小数位的值
- mysql常用命令
- Python单元测试——深入理解unittest
- GEF由代码轻松生成UML图
- 第七周 项目一——建立顺序环形队列算法库
- PAT 1088. Rational Arithmetic (20)(分数加减乘除)(待修改)
- Hdfs 压缩
- FileApi的文件上传基础
- Swift - 修改顶部状态栏(statusBar)文字颜色为白色
- java 怎么向Excel中加入超链接,指向相对路径的文件
- Ubuntu 16.04 下 Qt 5.6 无法输入中文的问题
- STM32 数据类型定义
- leetCode练习(49)
- 第五周项目1-建立顺序栈算法库