1088. Rational Arithmetic (20)
2015-08-16 16:08
330 查看
题目地址 http://www.patest.cn/contests/pat-a-practise/1088
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等),多验证几个就行了
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等),多验证几个就行了
#include <stdio.h> #include <iostream> #include <stdlib.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <map> #include <unordered_map> #include <queue> #include <vector> #include <set> #include <iterator> using namespace std; #define N 100001 long int a1, b1, a2, b2; long int gcd(long int m, long int n) { if (m < n) { long int tmp = m; m = n; n = tmp; } long int r = m % n; while (r != 0) { m = n; n = r; r = m % n; } return n; } void pf(long int a1, long int b1) { if (a1 == 0) { printf("0"); return; } int flag = 1; if (a1 < 0) { flag = -1; a1 = -a1; } long int gcdd = gcd(a1, b1); a1 /= gcdd; b1 /= gcdd; if (flag == -1) { printf("(-"); } if (a1 >= b1) { long int tmp = a1 / b1; a1 = a1 % b1; printf("%ld", tmp); if (a1 > 0) { printf(" %ld/%ld", a1, b1); } else{ } } else{ printf("%ld/%ld", a1, b1); } if (flag == -1) { printf(")"); } } void add() { long int aa = a1 * b2 + a2*b1; long int bb = b1 * b2; pf(aa, bb); } void sub() { long int aa = a1 * b2 - a2*b1; long int bb = b1 * b2; pf(aa, bb); } void cheng() { long int aa = a1*a2; long int bb = b1 * b2; pf(aa, bb); } void chu() { if (a2 == 0) { printf("Inf"); } else if (a2 < 0){ a1 = -a1; a2 = -a2; long int aa = a1*b2; long int bb = b1 * a2; pf(aa, bb); } else{ long int aa = a1*b2; long int bb = b1 * a2; pf(aa, bb); } } int main() { //freopen("in", "r", stdin); while (scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2) != EOF) { pf(a1, b1); printf(" + "); pf(a2, b2); printf(" = "); add(); printf("\n"); pf(a1, b1); printf(" - "); pf(a2, b2); printf(" = "); sub(); printf("\n"); pf(a1, b1); printf(" * "); pf(a2, b2); printf(" = "); cheng(); printf("\n"); pf(a1, b1); printf(" / "); pf(a2, b2); printf(" = "); chu(); printf("\n"); } return 0; }
相关文章推荐
- Windows下为64位的python3.4.3安装numpy
- R语言系列:datasets(R自带数据包)
- linux学习之路(7)
- 教你用200行Python代码“换脸”
- linux学习之软链接和硬链接
- 五种内部类形式将线程隐藏于类中
- 在Win10上使用Visual Studio2015的Android模拟器
- CodeVS 第二次月赛 A
- poj 2516 Minimum Cost
- Pki原则
- JS 实现继承静态属性
- 判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
- POJ1043 What's In a Name 逆图+完美匹配
- new与delete函数使用方法以及注意事项
- 理论物理极础7:对称性与守恒定律
- php:文件系统③
- Python 的 Numpy SciPy MatPlotLib配置
- java.lang.AbstractMethodError: gnu.xml.dom.DomDocument.getXmlStandalone()Z
- jQuery.lazyload详解
- combination sum