1088. Rational Arithmetic (20)
2016-02-18 02:34
405 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
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 <vector> #include <algorithm> using namespace std; long long lcd(long long a,long long b) { if(a< b) { long long t=a; a=b; b=t; } long long r = a%b; while(r) { a=b; b=r; r=a%b; } return b; } inline long long lcm(long long a,long long b){return a*b/lcd(a,b);} void print(long long n,long long d) { bool sign=false; if(n < 0) { n=-n; sign=true; } long long i=n/d; //integer n%=d; //fraction if( n != 0) { long long divisor=lcd(n,d); n/=divisor; d/=divisor; } if(sign) { if( i == 0)n=-n; else i=-i; } if( i == 0 && n == 0) { cout<<0; } else if( i != 0 && n == 0) { if(i<0)cout<<"("<<i<<")"; else cout<<i; } else if( i != 0 && n != 0) { if(i<0)cout<<"("<<i<<" "<<n<<"/"<<d<<")"; else cout<<i<<" "<<n<<"/"<<d; } else { if(n < 0)cout<<"("<<n<<"/"<<d<<")"; else cout<<n<<"/"<<d; } } int main() { long long numerator1,denominator1,numerator2,denominator2,res_numerator,res_denominator; cin>>numerator1;getchar();cin>>denominator1; cin>>numerator2;getchar();cin>>denominator2; // + res_denominator=lcm(denominator1,denominator2); res_numerator=res_denominator/denominator1*numerator1+res_denominator/denominator2*numerator2; print(numerator1,denominator1);cout<<" + ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl; // - res_numerator=res_denominator/denominator1*numerator1-res_denominator/denominator2*numerator2; print(numerator1,denominator1);cout<<" - ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl; // * res_numerator=numerator1 * numerator2;res_denominator=denominator1*denominator2; print(numerator1,denominator1);cout<<" * ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl; // / if(numerator2 != 0) { if(numerator1 * numerator2 >= 0) { res_numerator=numerator1*denominator2>0?numerator1*denominator2:-numerator1*denominator2; res_denominator=denominator1*numerator2>0?denominator1*numerator2:-denominator1*numerator2; } else { res_numerator=numerator1*denominator2>0?-numerator1*denominator2:numerator1*denominator2; res_denominator=denominator1*numerator2>0?denominator1*numerator2:-denominator1*numerator2; } print(numerator1,denominator1);cout<<" / ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl; } else {print(numerator1,denominator1);cout<<" / ";print(numerator2,denominator2);cout<<" = ";cout<<"Inf";cout<<endl;} system("pause"); return 0; }
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)