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 ...
随机推荐
- Day 5-4封装.__隐藏属性或者方法
封装 property 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏. 在python中用双下划线开头的方式将属性隐藏起来(设置成 ...
- 在JavaEE中使用Mybatis框架
MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录.每个MyB ...
- Django--权限信息操作
一 . 权限控制 表结构的设计 rbca(Role Based Access Control) 基于角色的权限控制 3个model 5张表 class User(models.Model): # ...
- linux audit审计(4)--audit的日志切分,以及与rsyslog的切分协同使用
audit的规则配置稍微不当,就会短时间内产生大量日志,所以这个规则配置一定要当心.当audit日志写满后,可以看到如下场景: -r-------- 1 root root 8388609 Mar 3 ...
- python数据结构与算法第十七天【概率算法】
1. 古典概率 例如:麻将开始摸到的14张牌中无将的概率,两张相同的牌即为将,则有: 所有的情况:从136张牌中选出14张牌,为C136-14 无将的情况:将不同的牌分组,共有34组,依次取14张牌, ...
- table2excel使用
原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...
- How to write to an event log by using Visual C#
using System; using System.Diagnostics; namespace WriteToAnEventLog_csharp { /// Summary description ...
- C语言实现字符串逆序输出
方法一: #include <stdio.h> #include <stdlib.h> #include <string.h> void Reverse(char ...
- BZOJ2829信用卡凸包——凸包
题目描述 输入 输出 样例输入 2 6.0 2.0 0.0 0.0 0.0 0.0 2.0 -2.0 1.5707963268 样例输出 21.66 提示 本样例中的2张信用卡的轮廓在上图中用实线标出 ...
- Vuex以及axios
Vuex 简介 vuex是一个专门为Vue.js设计的集中式状态管理架构. 状态? 我们把它理解为在data中需要共享给其他组件使用的部分. Vuex和单纯的全局对象有以下不同: 1.Vuex 的状态 ...