PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题。
注意:读入的分数可能不是最简的。输出时也需要转换成最简。
#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)的更多相关文章
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 【PAT甲级】1088 Rational Arithmetic (20 分)
题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642
PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- PAT (Advanced Level) Practice 1035 Password (20 分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- 【PAT Advanced Level】1008. Elevator (20)
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...
- PAT (Advanced Level) 1112. Stucked Keyboard (20)
找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...
随机推荐
- 修改visual studio setup 安装顺序(解决新版安装包无法自动移除老版本程序的问题)
背景 visual studio setup 支持自动删除之前版本的安装,需要设置RemovePreviousVersions = true, DetectNewerInstalledVersion ...
- msys2 使用指定boost
pacman -S mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-openssl mingw-w64- ...
- shell脚本,批量创建10个系统帐号并设置密码为随机8位字符串。
[root@localhost wyb]# cat user10.sh #!/bin/bash #批量创建10个系统帐号wangyb01-wangyb10并设置密码(密码为随机8位字符串). > ...
- shell脚本,按字母出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- 长链剖分优化dp三例题
首先,重链剖分我们有所认识,在dsu on tree和数据结构维护链时我们都用过他的性质. 在这里,我们要介绍一种新的剖分方式,我们求出这个点到子树中的最长链长,这个链长最终从哪个儿子更新而来,那个儿 ...
- Maven运行报错:-Dmaven.multiModuleProjectDirectory system propery is not set.
eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- Handler处理器和自定义Opener
Handler处理器 和 自定义Opener opener是 urllib2.OpenerDirector 的实例,我们之前一直都在使用的urlopen,它是一个特殊的opener(也就是模块帮我们构 ...
- adb 常用命令详解
1.把电脑上文件或目录copy到手机中:adb push <local> <remote> - copy file/dir to device 此处的<local& ...
- struts 乱码
在进行struts开发的过程中,总也是出现很多的乱码问题 ,但归根到底,也只是以下三种情况: ㈠页面显示中文乱码 ㈡传递参数中文乱码 ㈢国际化资源文件乱码 下面就这三中情况介绍怎么在具体项目中处理这些 ...
- Hive 启动报错 URI
Exception in thread "main"java.lang.RuntimeException: java.lang.IllegalArgumentException:j ...