PAT A1088. Rational Arithmetic (20)
来源:互联网 发布:plsql查看存储过程源码 编辑:程序博客网 时间:2024/05/22 03:52
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/2Sample Output 1:
2/3 + (-2) = (-1 1/3)2/3 - (-2) = 2 2/32/3 * (-2) = (-1 1/3)2/3 / (-2) = (-1/3)Sample Input 2:
5/3 0/6Sample Output 2:
1 2/3 + 0 = 1 2/31 2/3 - 0 = 1 2/31 2/3 * 0 = 01 2/3 / 0 = Inf
分数四则运算,除法前记得特判0的情况。
题解:
#include <cstdio>#include <algorithm>using namespace std;struct Fraction{long long up, down;}f1,f2;long long gcd(long long a, long long b){if(b == 0) return a;else return gcd(b, a % b);}Fraction reduction(Fraction c){if(c.down < 0){c.up = -c.up;c.down = -c.down;}if(c.up == 0) c.down = 1;else{long long d = gcd(abs(c.up), c.down);c.up /= d;c.down /= d;}return c;}Fraction add(Fraction a, Fraction b){Fraction c;c.up = a.up * b.down + a.down * b.up;c.down = a.down * b.down;return reduction(c);}Fraction sub(Fraction a, Fraction b){Fraction c;c.up = a.up * b.down - a.down * b.up;c.down = a.down * b.down;return reduction(c);}Fraction mul(Fraction a, Fraction b){Fraction c;c.up = a.up * b.up;c.down = a.down * b.down;return reduction(c);}Fraction div(Fraction a, Fraction b){Fraction c;c.up = a.up * b.down;c.down = a.down * b.up;return reduction(c);}void print(Fraction a){a = reduction(a);if(a.up < 0) printf("(");if(a.down == 1) printf("%lld", a.up);else if(abs(a.up) > a.down) printf("%lld %lld/%lld", a.up/a.down, abs(a.up)%a.down, a.down);else printf("%lld/%lld", a.up, a.down);if(a.up < 0) printf(")");}int main(){scanf("%lld/%lld %lld/%lld", &f1.up, &f1.down, &f2.up, &f2.down);f1 = reduction(f1), f2 = reduction(f2);print(f1);printf(" + ");print(f2);printf(" = ");print(add(f1,f2));printf("\n");print(f1);printf(" - ");print(f2);printf(" = ");print(sub(f1,f2));printf("\n");print(f1);printf(" * ");print(f2);printf(" = ");print(mul(f1,f2));printf("\n");print(f1);printf(" / ");print(f2);printf(" = ");if(f2.up == 0) printf("Inf");else print(div(f1,f2));printf("\n");return 0;}
- pat-a1088. Rational Arithmetic (20)
- PAT A1088 Rational Arithmetic (20)
- PAT A1088. Rational Arithmetic (20)
- PAT A1088 rational arithmetical (20)
- PAT Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 【PAT】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 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) 模块化
- PAT 甲级 1088. Rational Arithmetic (20)
- Python实现Pat 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic
- POJ
- 洛谷 P1463 [SDOI2005]反素数ant && P1820 寻找AP数
- 单例设计模式之破坏
- 《Java从小白到大牛精简版》之第1章 开篇综述
- 学习笔记之面向对象编程18(流之 InputStream,OutputStream)
- PAT A1088. Rational Arithmetic (20)
- Python-pip的安装和配置
- 黑苹果无线网卡的三种解决方案
- Win 7/10安装numpy、pandas、scipy和matplotlib
- 20170915
- web前端常用网站网址
- 定时换图
- 代理模式接触
- hdu 6025 Coprime Sequence