PAT-A 1088. Rational Arithmetic
来源:互联网 发布:利达编程软件 编辑:程序博客网 时间:2024/05/17 07:44
1088. Rational Arithmetic
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/32/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/31 2/3 - 0 = 1 2/31 2/3 * 0 = 01 2/3 / 0 = Inf
程序代码:
#include<stdio.h>long long divisor(long long a,long long b);void print(long long a,long long b);void add(long long a,long long b,long long c,long long d);void minus(long long a,long long b,long long c,long long d);void division(long long a,long long b,long long c,long long d);void multiply(long long a,long long b,long long c,long long d);int main(){ long long a,b,c,d; scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d); add(a,b,c,d); minus(a,b,c,d); multiply(a,b,c,d); division(a,b,c,d); return 0;}void add(long long a,long long b,long long c,long long d){ print(a,b); printf(" + "); print(c,d); printf(" = "); print(a*d+b*c,b*d); printf("\n"); }void minus(long long a,long long b,long long c,long long d){ print(a,b); printf(" - "); print(c,d); printf(" = "); print(a*d-b*c,b*d); printf("\n");}void multiply(long long a,long long b,long long c,long long d){ print(a,b); printf(" * "); print(c,d); printf(" = "); print(a*c,b*d); printf("\n");}void division(long long a,long long b,long long c,long long d){ long long m= a*d; long long n= b*c; print(a,b); printf(" / "); print(c,d); printf(" = "); if(n==0) printf("Inf\n"); else { if(n<0) { m=m*-1; //把负号调整到分子上 n=n*-1; } print(m,n); printf("\n"); }}void print(long long a,long long b)//打印函数{ long long tmp; int flag =0; if(a==0) { printf("0"); return ; } else if(a<0)//不管分子为正还是为负,统统按照正整数处理 { a= -a; flag = 1;//标记为负数 } tmp = divisor(a,b);//求出最大公约数 a=a/tmp;//化为最简形式 b=b/tmp; long c=a/b;//整数部分 if(flag) { if(b==1) printf("(-%lld)",a); else if(c>0) printf("(-%lld %lld/%lld)",c,(a%b),b); else printf("(-%lld/%lld)",a%b,b); } else { if(b==1) printf("%lld",a); else if(c>0) printf("%lld %lld/%lld",c,a%b,b); else printf("%lld/%lld",a%b,b); }}long long divisor(long long a,long long b)//辗转相除法求最大公约数{ long long max = a>b?a:b; long long min =a<b?a:b; long long r=max%min; while(r) { max=min; min = r; r = max%min; } return min;}
- PAT-A 1088. Rational Arithmetic
- PAT A 1088. Rational Arithmetic (20)
- PAT-A 1088. Rational Arithmetic (20)
- PAT-A-1088. Rational Arithmetic (20)
- Pat(A) 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic
- 【PAT】1088. Rational Arithmetic
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 【PAT】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- PAT(A) 1088. Rational Arithmetic & PAT(B) 1034. 有理数四则运算(20)
- PAT 1088 1088. Rational Arithmetic (模拟四则运算)
- 【PAT】【Advanced Level】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20) 模块化
- PAT 甲级 1088. Rational Arithmetic (20)
- Python实现Pat 1088. Rational Arithmetic (20)
- PAT Rational Arithmetic (20)
- H5引入百度地图
- 总结 : volley超时和重复请求问题
- mysql 启动问题总结:the server quit without updating PID file(转)
- poj 1160 post office------DP
- 1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
- PAT-A 1088. Rational Arithmetic
- nyoj 104 最大和 51nod oj 1051 最大子矩阵和 【DP】
- C6678 H.264 高性能视频编码测试——基于网口
- windows-DLL注入
- 跟我学Redis(16)—Redis单主机多实例
- 类加载器
- C语言编译常见错误
- 【编程题】股票涨价的问题
- 51nod oj 1086 背包问题 V2 【多重背包问题】