PAT 甲级 1088. Rational Arithmetic (20)
来源:互联网 发布:js获取class对象的值 编辑:程序博客网 时间:2024/05/21 18:38
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 <iostream>#include <cstdio>#include <algorithm>using namespace std; typedef long long ll; ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);} struct Fraction{ ll up,down;}a,b; Fraction reduction(Fraction result){ if(result.down<0){ result.up=-result.up; result.down=-result.down; } if(result.up==0){ result.down=1; }else{ int d=gcd(abs(result.up),abs(result.down)); result.up/=d; result.down/=d; } return result;} Fraction add(Fraction a,Fraction b){ Fraction result; result.up=a.up*b.down+b.up*a.down; result.down=a.down*b.down; return reduction(result);} Fraction minu(Fraction a,Fraction b){ Fraction result; result.up=a.up*b.down-b.up*a.down; result.down=a.down*b.down; return reduction(result);} Fraction multi(Fraction a,Fraction b){ Fraction result; result.up=a.up*b.up; result.down=a.down*b.down; return reduction(result);} Fraction divide(Fraction a,Fraction b){ Fraction result; result.up=a.up*b.down; result.down=a.down*b.up; return reduction(result);} void showResult(Fraction r){ r=reduction(r); if(r.up<0) printf("("); if(r.down==1) printf("%lld",r.up); else if(abs(r.up)>r.down){ printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down); }else{ printf("%lld/%lld",r.up,r.down); } if(r.up<0) printf(")");} int main(){ scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down); showResult(a); printf(" + "); showResult(b); printf(" = "); showResult(add(a,b)); printf("\n"); showResult(a); printf(" - "); showResult(b); printf(" = "); showResult(minu(a,b)); printf("\n"); showResult(a); printf(" * "); showResult(b); printf(" = "); showResult(multi(a,b)); printf("\n"); showResult(a); printf(" / "); showResult(b); printf(" = "); if(b.up==0)printf("Inf"); else showResult(divide(a,b)); printf("\n"); return 0;}
- PAT 甲级 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)-PAT甲级真题
- [转]PAT甲级练习1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 【PAT】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 1034. 有理数四则运算(20)PAT乙级&&1088. Rational Arithmetic (20)PAT甲级
- PAT 1088. Rational Arithmetic
- 【PAT】1088. Rational Arithmetic
- PAT Rational Arithmetic (20)
- 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】【Advanced Level】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20) 模块化
- Python实现Pat 1088. Rational Arithmetic (20)
- C#解析xml文件获取中国的省市县地区名称和zipcode编号
- mouseenter与mouseover的区别
- Maven_项目搭建启动问题_pom文件依赖出现问题
- 题(problem)(详解10.5hu测T3:Catalan)
- 【Halcon教程4】halcon/c++接口基础 之 析构函数和Halcon算子
- PAT 甲级 1088. Rational Arithmetic (20)
- cartogarpher slam 4
- 数据库水平切分的实现原理解析——分库,分表,主从,集群,负载均衡器(转)
- 【Halcon教程5】 halcon/c++接口基础 之内存管理
- 学习Docker(2017-10-4)
- volatile的详细用法
- 二叉搜索树(Binary Search Tree)
- mybatis报错:java.lang.IllegalArgumentException: Mapped Statements collection does not contain
- 【Halcon教程6】halcon/c++接口基础 之异常处理