A1088. 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/3
2/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/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct NODE{
long long up, dn;
int inf;
NODE(){
inf = ;
}
}node;
long long gcd(long long a, long long b){
if(b == )
return a;
else return gcd(b, a % b);
}
node add(node a, node b){
node temp;
temp.dn = a.dn * b.dn;
temp.up = a.up * b.dn + b.up * a.dn;
if(temp.dn < ){
temp.up *= -;
temp.dn *= -;
}
long long fac = gcd(abs(temp.up), abs(temp.dn));
if(fac != ){
temp.up = temp.up / fac;
temp.dn = temp.dn / fac;
}
return temp;
}
node multp(node a, node b){
node temp;
temp.dn = a.dn * b.dn;
temp.up = a.up * b.up;
if(temp.dn < ){
temp.up *= -;
temp.dn *= -;
}
long long fac = gcd(abs(temp.up), abs(temp.dn));
if(fac != ){
temp.up = temp.up / fac;
temp.dn = temp.dn / fac;
}
return temp;
}
node div(node a, node b){
node temp;
temp.dn = a.dn * b.dn;
temp.up = a.up * b.up;
if(temp.dn == ){
temp.inf = ;
return temp;
}
if(temp.dn < ){
temp.up *= -;
temp.dn *= -;
}
long long fac = gcd(abs(temp.up), abs(temp.dn));
if(fac != ){
temp.up = temp.up / fac;
temp.dn = temp.dn / fac;
}
return temp;
}
void show(node a){
if(a.inf == ){
printf("Inf");
}else{
if(a.up < ){
printf("(");
}
if(a.up == ){
printf("");
return;
}
if(abs(a.up) >= abs(a.dn)){
if(abs(a.up) % abs(a.dn) == ){
printf("%lld", a.up / a.dn);
}else{
printf("%lld %lld/%lld", a.up / a.dn, abs(a.up) % abs(a.dn), a.dn);
}
}else{
printf("%lld/%lld", a.up, a.dn);
}
if(a.up < )
printf(")");
}
}
int main(){
long long temp1;
node a, b, c, d;
scanf("%lld/%lld %lld/%lld", &a.up, &a.dn, &b.up, &b.dn);
long long fac = gcd(abs(a.up), abs(a.dn));
if(fac != ){
a.up = a.up / fac;
a.dn = a.dn / fac;
}
fac = gcd(abs(b.up), abs(b.dn));
if(fac != ){
b.up = b.up / fac;
b.dn = b.dn / fac;
}
c = b; c.up *= -;
d = b; swap(d.up, d.dn);
node re1 = add(a, b);
show(a); printf(" + "); show(b); printf(" = "); show(re1);
printf("\n");
node re2 = add(a, c);
show(a); printf(" - "); show(b); printf(" = "); show(re2);
printf("\n");
node re3 = multp(a, b);
show(a); printf(" * "); show(b); printf(" = "); show(re3);
printf("\n");
node re4 = div(a, d);
show(a); printf(" / "); show(b); printf(" = "); show(re4);
cin >> temp1;
return ;
}
总结:
1、只有除法需要检测INF, 乘法不需要。
2、本题需要输出 题目中输入的数字,所以当输入的分数不是最简分数时,需要先将输入化简,再计算、输出。
A1088. Rational Arithmetic的更多相关文章
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
随机推荐
- Prism框架研究(三)
这一篇主要用来介绍一下基于Prism Library中的核心服务以及如何配置Container,还有一个重要的部分是如何管理各个组件之间的依赖性,下面就这些内容来做一一的介绍. 1 Prism中的核心 ...
- PHP的爬虫框架
Beanbun PHPSpider PHPQuery QueryList PHPCrawer Snoopy
- Log4j2配置与使用
依赖包: <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <depend ...
- vue-resource: jsonp请求百度搜索的接口
1. yarn add vue-resource 2. main.js引入vue-resource import Vue from 'vue' import MintUI from 'mint-ui' ...
- PHP关联查询
article文章表: aid title content uid 1 文章1 文章1正文内容... 1 2 文章2 文章2正文内容... 1 3 文章3 文章3正文内容... 2 4 文章4 文章4 ...
- How to remove ROM in MAME
/usr/share/games/mame/roms/ /usr/local/share/games/mame/roms/ sudo rm /usr/local/share/games/mame/ro ...
- nginx 正向代理上网
配置文件: server { #resolver 21.202.152.10; #指定DNS服务器IP地址 |如果指定IP$scheme://22.2.65.214$request_uri 可以不指定 ...
- Lambda 动态表达式(排序)
网上看到的: class Program { static List<User> list = new List<User>() { new User(){ID=1,Name= ...
- BZOJ 1912 巡逻(算竞进阶习题)
树的直径 这题如果k=1很简单,就是在树的最长链上加个环,这样就最大化的减少重复的路程 但是k=2的时候需要考虑两个环的重叠部分,如果没有重叠部分,则和k=1的情况是一样的,但是假如有重叠部分,我们可 ...
- MT【169】拉格朗日配方
已知$x^2+y^2+z^2=1$求$3xy-3yz+2z^2$的最大值______ 答案:$3$ 提示:$3(x^2+y^2+z^2)-(3xy-3yz+2z^2)=3\left(y+\dfrac{ ...