1088. Rational Arithmetic (20)
来源:互联网 发布:北京广联达软件多少钱 编辑:程序博客网 时间:2024/06/05 12:01
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:
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.
IDEA
1.关键在于化简分数,需要找到两个数a,b的最大公约数comm_divisor()
2.若a是负数,需要输出括号和符号等
CODE
#include<cstdio>int comm_divisor(long long a,long long b){long long min,max;min=(a>b)?b:a;max=(a>b)?a:b;long long r=max%min;while(r){max=min;min=r;r=max%min;}return min;}void output(long long a,long long b){int flag=0;if(a<0){a=-a;flag=1;//printf("fu");}if(!a){printf("0");}else{long long div=comm_divisor(a,b);a/=div;b/=div;long long c=a/b;long long d=a%b;if(flag){//负的 if(d==0){printf("(-%lld)",c);}else{if(c==0){printf("(-%lld/%lld)",a,b);}else{printf("(-%lld %lld/%lld)",c,d,b);}} }else{if(d==0){printf("%lld",c);}else{if(c==0){printf("%lld/%lld",a,b);}else{printf("%lld %lld/%lld",c,d,b);}} }}}void add(long long a1,long long b1,long long a2,long long b2){output(a1,b1);printf(" + ");output(a2,b2);printf(" = ");output(a1*b2+a2*b1,b1*b2);printf("\n");}void minus(long long a1,long long b1,long long a2,long long b2){output(a1,b1);printf(" - ");output(a2,b2);printf(" = ");output(a1*b2-a2*b1,b1*b2);printf("\n");}void muilt(long long a1,long long b1,long long a2,long long b2){output(a1,b1);printf(" * ");output(a2,b2);printf(" = ");output(a1*a2,b1*b2);printf("\n");}void divide(long long a1,long long b1,long long a2,long long b2){output(a1,b1);printf(" / ");output(a2,b2);printf(" = ");if(a2==0) printf("Inf\n");else{if(a2<0){a2=-a2;a1=-a1;}output(a1*b2,b1*a2);printf("\n");}}int main(){long long a1,b1,a2,b2;scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);add(a1,b1,a2,b2);minus(a1,b1,a2,b2);muilt(a1,b1,a2,b2);divide(a1,b1,a2,b2);return 0;}
0 0
- 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)
- ECMAScript 6笔记(let,const 和 变量的解构赋值)
- Linux文件系统简介
- [Leetcode] 92. Reverse Linked List II
- iOS App沙盒模型文件目录详解Swift版
- zb的生日
- 1088. Rational Arithmetic (20)
- epoll和poll、select的区别
- shell学习笔记(一)
- XSS注入我也不怕不怕啦——PHP从框架层面屏蔽XSS的思考和实践
- Android Studio找不到R文件解决方法
- 解救人质最小步数
- linux命令mv:移动文件、文件重命名
- wamp安装提示缺少msvcr110.dll的解决办法
- OJ提交题目中的语言选项里G++与C++的区别