简单题。

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

#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. Bootstrap历练实例:超小的按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  2. ratio_to_report分析函数求占比

    drop table test; create table test ( name varchar(20), kemu varchar(20), score number  ); insert int ...

  3. java 操作mongodb查询条件的常用设置

    java操作mongodb进行查询,常用筛选条件的设置如下: 条件列表:BasicDBList condList = new BasicDBList(); 临时条件对象:BasicDBObject c ...

  4. 如何用纯 CSS 绘制一个充满动感的 Vue logo

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/zaqKPx 可交互视频教 ...

  5. python基础学习笔记——迭代器

    我们之前一直在用可迭代对象进行操作,那么到底什么是可迭代对象.我们现在就来讨论讨论可迭代对象.首先我们先回顾下我们 熟知的可迭代对象有哪些: str  list   tuple  dic  set  ...

  6. Django中的tags,母版和继承,组件,静态文件相关,自定义simpletag和自定义inclusion_tag

    Tags for <ul> {% for user in user_list %} <li>{{ user.name }}</li> {% endfor %} &l ...

  7. vmware安装CentOS " Intel VT-x 处于禁用状态"

    我刚用虚拟机vmware 11安装CentOS 7 ,出现错误“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题,如下图. Intel VT-x 即Virtualiza ...

  8. Java&Android代码规范

    项目中直接导入Square的代码风格文件.(不导入Google的原因是Square同时提供了Java和Android两套统一风格,Google只提供了一套) Square Code Styles Go ...

  9. 最近项目中公用的JS

    var closeid = 1; var isneedpwd = 0; var editor1; var NoCheckUrl = 0;//适用于框架 不验证权限 !=0验证 function Erp ...

  10. 关于ul中li不对齐的问题

    将li中加入 overflow:hidden;    即可. 同时overflow:auto  可以控制滚动条的出现.