简单题。

注意:读入的分数可能不是最简的。输出时也需要转换成最简。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; struct FS
{
long long fz,fm;
FS(long long a,long long b)
{
fz=a;
fm=b;
}
}; long long gcd(long long a,long long b)
{
if(b==) return a;
return gcd(b,a%b);
} FS change(FS res)
{
if(res.fz!=)
{
long long GCD=gcd(abs(res.fz),abs(res.fm));
res.fz=res.fz/GCD;
res.fm=res.fm/GCD;
} else
{
res.fz=;
res.fm=;
} return res;
} FS ADD(FS a,FS b)
{
FS res(,); res.fz=a.fz*b.fm+b.fz*a.fm;
res.fm=a.fm*b.fm; res=change(res);
return res;
} FS SUB(FS a,FS b)
{
FS res(,); res.fz=a.fz*b.fm-b.fz*a.fm;
res.fm=a.fm*b.fm; res=change(res);
return res;
} FS MUL(FS a,FS b)
{
FS res(,); res.fz=a.fz*b.fz;
res.fm=a.fm*b.fm; res=change(res);
return res;
} FS DIV(FS a,FS b)
{
FS res(,); if(b.fz==)
{
res.fz=;
res.fm=;
return res;
} if(a.fz==) return res; res.fz=a.fz*b.fm;
res.fm=a.fm*b.fz; if(res.fm<)
{
res.fm=-res.fm;
res.fz=-res.fz;
} res=change(res);
return res;
} void output(FS a)
{
if(a.fm==)
{
printf("Inf");
return;
} if(a.fz==)
{
printf("");
return;
} a=change(a);
if(abs(a.fz)<a.fm)
{
if(a.fz<) printf("("); printf("%lld/%lld",a.fz,a.fm);
if(a.fz<) printf(")");
return;
} if(a.fz>)
{
if(a.fz%a.fm==)
{
printf("%lld",a.fz/a.fm);
return;
}
else
{
printf("%lld %lld/%lld",a.fz/a.fm,a.fz%a.fm,a.fm);
return;
}
} else
{
a.fz=-a.fz; printf("(");
if(a.fz%a.fm==)
{
printf("-%lld",a.fz/a.fm);
printf(")");
return;
}
else
{
printf("-%lld %lld/%lld",a.fz/a.fm,a.fz%a.fm,a.fm);
printf(")");
return;
} }
} int main()
{
long long s1,s2,s3,s4;
scanf("%lld/%lld %lld/%lld",&s1,&s2,&s3,&s4); FS a(s1,s2);
FS b(s3,s4); output(a); cout<<" + "; output(b); cout<<" = "; output(ADD(a,b));
cout<<endl;
output(a); cout<<" - "; output(b); cout<<" = "; output(SUB(a,b));
cout<<endl;
output(a); cout<<" * "; output(b); cout<<" = "; output(MUL(a,b));
cout<<endl;
output(a); cout<<" / "; output(b); cout<<" = "; output(DIV(a,b));
cout<<endl; return ;
}

PAT (Advanced Level) 1088. Rational Arithmetic (20)的更多相关文章

  1. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

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

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

  3. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  4. PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...

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

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

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

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

  7. PAT (Advanced Level) Practice 1035 Password (20 分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  8. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

  9. PAT (Advanced Level) 1112. Stucked Keyboard (20)

    找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...

随机推荐

  1. a标签目标链接问题

    1.先确定开始文件和目标文件,例如从css.html开始到body.html 2.确定文件寻找路径,因为css.html的父目录是css,而body.html在body目录下,所以需要先退到上一目录h ...

  2. Linux内核漏洞利用-环境配置(转)

    实验环境: Ubuntu-14.04.1 x86 linux-2.6.32.1 busybox-1.27.2 qemu 0x00 安装qemu sudo apt-get install qemu qe ...

  3. PAT (Basic Level) Practise (中文)-1028. 人口普查(20)

    PAT (Basic Level) Practise (中文)-1028. 人口普查(20)   http://www.patest.cn/contests/pat-b-practise/1028 某 ...

  4. DP玄学优化——斜率优化

    --以此博客来悼念我在\(QBXT\)懵逼的时光 \(rqy\; tql\) (日常%\(rqy\)) 概念及用途 斜率优化是\(DP\)的一种较为常用的优化(据说在高中课本里稍有提及),它可以用于优 ...

  5. Noip2011提高组 聪明的质监员

    题目传送门 讲真,既然质监员这么聪明,为什么要让我们帮他设计程序? 所以还是叫ZZ的质检员吧 其实,我最想说的,不是这个题,而是这个\(\Sigma\)(一见 \(\Sigma\) 就懵逼系列) 这个 ...

  6. Elasticsearchs的安装/laravel-scout和laravel-scout-elastic的安装

    安装: https://github.com/medcl/elasticsearch-rtf 先下载包 下载解压后 cd elasticsearch-rtf-master ll bin/elastic ...

  7. Python解答力扣网站题库简单版----第三讲

    1041. 困于环中的机器人 题库链接: 1041. 困于环中的机器人. 题干 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G" ...

  8. 递归函数&二分查找

    一.递归函数 1)定义 在函数中调用函数本身,就是递归 在python中递归的深度最大为1000,但实际达不到1000 def func(): print("-----func-----&q ...

  9. 算法学习记录-图——最短路径之Dijkstra算法

    在网图中,最短路径的概论: 两顶点之间经过的边上权值之和最少的路径,并且我们称路径上的第一个顶点是源点,最后一个顶点是终点. 维基百科上面的解释: 这个算法是通过为每个顶点 v 保留目前为止所找到的从 ...

  10. Java列出接口实现的所有接口

    package com.tj; public class MyClass2 { public static void main(String[] args) { Class cls = java.ut ...