1088. Rational Arithmetic (20)
来源:互联网 发布:python开发环境配置 编辑:程序博客网 时间:2024/06/06 01:29
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
#include <stdio.h>#include <math.h>long long gcd(long long a,long long b){long long t;for(t;t=b;b=a%b,a=t);return a;}void sim(long long a[]){long long a1,b1,i,g;a1 = llabs(a[0]);b1 = llabs(a[1]);g = gcd(a1,b1);if(g!=0){ a1 = a1/g; b1 = b1/g;}a[0] = (a[0]<0 ? -1:1)*a1;a[1] = (a[1]<0 ? -1:1)*b1;}void output(long long a[]){int flag=0;if(a[0] == 0){printf("0");return;}if(a[1] == 0){printf("Inf");return;}sim(a);if(a[0]*a[1]<0){flag = 1;}if(flag){printf("(-");}if(a[0]/a[1]!=0){if(a[0]%a[1]==0){printf("%lld",llabs(a[0]/a[1]));if(flag){putchar(')');}return;}else{printf("%lld %lld/%lld",llabs(a[0]/a[1]),llabs(a[0]%a[1]),llabs(a[1]));}}else{printf("%lld/%lld",llabs(a[0]),llabs(a[1]));}if(flag){putchar(')');}}int main(){long long a[2],b[2],c[2];scanf("%lld/%lld",&a[0],&a[1]);scanf("%lld/%lld",&b[0],&b[1]);output(a);printf(" + ");output(b);printf(" = ");c[1] = a[1]*b[1];c[0] = a[1]*b[0]+a[0]*b[1];output(c);putchar('\n');output(a);printf(" - ");output(b);printf(" = ");c[1] = a[1]*b[1];c[0] = a[0]*b[1]-a[1]*b[0];output(c);putchar('\n');output(a);printf(" * ");output(b);printf(" = ");c[1] = a[1]*b[1];c[0] = a[0]*b[0];output(c);putchar('\n');output(a);printf(" / ");output(b);printf(" = ");c[1] = a[1]*b[0];c[0] = a[0]*b[1];output(c);putchar('\n');return 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)
- HDU 2553 N皇后问题
- 文件存储openFileOutput和openFileInput
- zookeeper管理控制台安装
- bower使用入门
- [JQ权威指南]使用JQ读取JSON数据
- 1088. Rational Arithmetic (20)
- 构造性证明
- C#无边框移动
- mysql技巧
- 定义一个只能在堆上(栈上)生成对象的类
- 称砝码******
- C#无边框移动
- Volley 源码解析
- SQL语句优化