1088. Rational Arithmetic (20)
来源:互联网 发布:江苏计算机二级vb真题 编辑:程序博客网 时间:2024/06/01 09:33
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 oflong 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
给出两个分数,分别求加减乘除运算的结果。
代码:
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>using namespace std;long long GCM(long long n,long long m){if(n<m) swap(n,m);if(m==0) return n;long long r=n%m;while(r!=0){n=m;m=r;r=n%m;}return m;}void str2num(string s,long long &num,long long &den){int sign=1;if(s[0]=='-'){sign=-1;s=s.substr(1);}int i=0;while(s[i]!='/') i++;num=atoll(s.substr(0,i).c_str());den=atoll(s.substr(i+1).c_str());long long gcm=GCM(num,den);num/=sign*gcm;den/=gcm;}void print(long long num,long long den){int sign=1;if(num<0&&den>0){sign=-1;num=-num;}else if(num>=0&&den<0){sign=-1;den=-den;}if(sign==-1) printf("(-");long long gcm=GCM(num,den);num/=gcm;den/=gcm;long long a=num/den,b=num%den,c=den;if(a==0&&b==0) printf("0");else if(a==0) printf("%lld/%lld",b,c);else if(b==0) printf("%lld",a);else printf("%lld %lld/%lld",a,b,c);if(sign==-1) printf(")");}int main(){long long num1,den1,num2,den2;string a,b;cin>>a>>b;str2num(a,num1,den1);str2num(b,num2,den2);long long gcm=GCM(den1,den2);print(num1,den1);printf(" + ");print(num2,den2);printf(" = ");print((num1*den2+num2*den1)/gcm,den1*den2/gcm);printf("\n");print(num1,den1);printf(" - ");print(num2,den2);printf(" = ");print((num1*den2-num2*den1)/gcm,den1*den2/gcm);printf("\n");print(num1,den1);printf(" * ");print(num2,den2);printf(" = ");print(num1*num2,den1*den2);printf("\n");print(num1,den1);printf(" / ");print(num2,den2);printf(" = ");if(num2==0) printf("Inf");else print(num1*den2,num2*den1);printf("\n");}
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- spring注解
- leetcode ----Intersection of Two Arrays
- 数据结构实验之链表二:逆序建立链表
- Jackson常用注解介绍
- 树莓派 让vim支持python
- 1088. Rational Arithmetic (20)
- 5个常用的排序算法总结
- hibernate连接mysql的编码设置
- Javabean转换成json字符串的工具类
- 安装tensorflow
- poj1321
- cocos2dx 项目拷贝遇到的一些问题
- RAC之masonry源码深度解析
- 环信SDK介绍及导入--官档精简3.x