1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
long long gcd(long long a,long long b){//不能保证返回的符号,但能保证返回的绝对值大小
if(b==){
if(a<){
a=-a;
}
return a;
}
return gcd(b,a%b);
}
long long com;
void output(long long fz1,long long fm1){
if(fz1==){
printf("");
return;
}
com=gcd(fz1,fm1);
fz1/=com;
fm1/=com; //cout<<fz1<<" "<<fm1<<" "<<com<<endl; if(fz1%fm1==){
printf("%lld",fz1/fm1);
}
else{
if(fz1/fm1){
printf("%lld ",fz1/fm1);
if(fz1<){
fz1=-fz1;
}
}
printf("%lld/%lld",fz1-fz1/fm1*fm1,fm1);
}
}
void add(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" + ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
com=gcd(fm1,fm2);
fz2*=fm1/com;
fz1*=fm2/com;
fm1*=fm2/com; //cout<<fz1<<" "<<fm1<<" "<<fz2<<" "<<fm2<<endl; printf(" = ");
fz1+=fz2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void sub(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" - ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
com=gcd(fm1,fm2);
fz2*=fm1/com;
fz1*=fm2/com;
fm1*=fm2/com;
printf(" = ");
fz1-=fz2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void mul(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" * ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
printf(" = ");
fz1*=fz2;
fm1*=fm2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void quo(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" / ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
printf(" = ");
fz1*=fm2;
fm1*=fz2;
if(fm1==){
printf("Inf");
return;
}
if(fm1<){
fm1=-fm1;
fz1=-fz1;
}
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
long long fz1,fm1,inter1,fz2,fm2,inter2;
scanf("%lld/%lld %lld/%lld",&fz1,&fm1,&fz2,&fm2); //cout<<fm2<<endl; com=gcd(fz1,fm1);
fz1/=com;
fm1/=com;
com=gcd(fz2,fm2); //cout<<com<<endl;
//cout<<fm2<<endl;
fz2/=com;
fm2/=com;
//cout<<fm2<<endl;
//计算
//cout<<fz1<<" "<<fm1<<" "<<fz2<<" "<<fm2<<endl;
add(fz1,fm1,fz2,fm2);
printf("\n");
sub(fz1,fm1,fz2,fm2);
printf("\n");
mul(fz1,fm1,fz2,fm2);
printf("\n");
quo(fz1,fm1,fz2,fm2);
printf("\n");
return ;
}

pat1088. Rational Arithmetic (20)的更多相关文章

  1. PAT1088:Rational Arithmetic

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  2. PAT Rational Arithmetic (20)

    题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...

  3. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  4. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  5. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  6. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  7. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  8. PAT_A1088#Rational Arithmetic

    Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...

  9. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...

随机推荐

  1. springcloud安全控制token的创建与解析

    import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorith ...

  2. Python3中的Json与Pickle

    用于将数据存取到文件中: 1:Json的存数据到文件: import json info = { "name":"hjc", "age":2 ...

  3. J2SE-鸡汤

    前言 终于开始学java了,心里免不了的开心,总是听老乡说他们公司搞java开发,用的什么什么框架,说的自己都有点眼馋了,但是根据自己的性格,不了解它,肯定不会就去用它的.所以在学习框架之前,java ...

  4. 解决织梦手机网站M文件夹动态游览不生成html

    今天的做手机网站的时候,发现dede织梦M文件夹下会生成index.html.对于手机网站来说,太麻烦了.每次更新手机网站首页都要把index.html删除掉重新生成. 然而织梦不支持手机网站首页动态 ...

  5. 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths

    P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...

  6. 使用Tensorflow object detection API——训练模型(Window10系统)

    [数据标注处理] 1.先将下载好的图片训练数据放在models-master/research/images文件夹下,并分别为训练数据和测试数据创建train.test两个文件夹.文件夹目录如下 2. ...

  7. 洛谷P2071 座位安排

    题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛,每排座位只能坐两人,且每个人都有自己想坐的排数,问最 ...

  8. Qt 学习之路 2(10):对象模型

    Home / Qt 学习之路 2 / Qt 学习之路 2(10):对象模型 Qt 学习之路 2(10):对象模型  豆子  2012年9月2日  Qt 学习之路 2  45条评论 标准 C++ 对象模 ...

  9. Javaweb 实现分页功能

    package cn.zr.testpage.entity; public class User { private String name; private int age; private Str ...

  10. Visual Studio 2017 Key激活码

     Microsoft Visual Studio Enterprise 2017 企业版 KEY:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFMicrosoft Visual Studi ...