PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; struct FenShu
{
long long fz,fm;
FenShu(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);
} FenShu ADD(FenShu a,FenShu b)
{
FenShu res(,);
res.fz=a.fz*b.fm+b.fz*a.fm;
res.fm=a.fm*b.fm; 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;
} int main()
{
int n; scanf("%d",&n);
FenShu ans(,);
for(int i=;i<=n;i++)
{
long long fz,fm; scanf("%lld/%lld",&fz,&fm);
FenShu t(fz,fm);
ans=ADD(ans,t);
}
//printf("%d/%d\n",ans.fz,ans.fm);
if(ans.fz==) printf("0\n");
else
{
if(ans.fz%ans.fm==) printf("%lld\n",ans.fz/ans.fm);
else if(abs(ans.fz)<ans.fm) printf("%lld/%lld\n",ans.fz,ans.fm);
else
{
long long d=ans.fz/ans.fm;
ans.fz=ans.fz-d*ans.fm;
printf("%lld %lld/%lld\n",d,ans.fz,ans.fm);
}
}
return ;
}
PAT (Advanced Level) 1081. Rational Sum (20)的更多相关文章
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- 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 Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- 1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- PAT (Advanced Level) Practice 1035 Password (20 分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
随机推荐
- oc汉子转拼音
oc中可以不使用第三方库直接吧数组转成拼音: 代码如下: NSString *str = @"中国abc人民共和国"; CFStringRef aCFString=(__bridg ...
- postgres-xl 集体搭建
pgxl 集群搭建 一 预备 1 下载安装解压源码 /opt/ curl -O http://files.postgres-xl.org/postgres-xl95r1beta1.tar.gz tar ...
- [转]phpcms 判断用户登录代码
<!-- 用户登录开始 --> <?php //读取用户信息 $ypzy_userid = param::get_cookie('_userid'); if(!empty($ypzy ...
- 命名空间“Microsoft”中不存在类型或命名空间名称“Office”(是缺少程序集引用吗?)
通过引用这个解决,不同的的office版本,中间的版本号可能不同,如图所示:
- iperf linux版本移植到android (使用工具链方式不是使用Android.mk)
由于很多程序是用makefile编译linux应用程序的,如果移植到android就要重新写Android.mk,对于不熟悉这个的人来说,特别麻烦,所以这里介绍只修改makefile就能移植到andr ...
- Linq序列间的操作
一.Union并集操作相同的数据只显示一次 二.Concact连接 相同的数据也会重复显示 三.except差集 我有的你没有的显示 四.intersect交集相同的显示 五.zip序列的合并 六.d ...
- 【jsp网站计数功能】 application session
在jsp页面中实现网站计数器的方法有很多,其中比较普遍的做法是利用application 和session对象.application对象可被所有用户共享:session是单用户共享,用户从访问系统开 ...
- Local declaration of 'XXX' hides instance variable
今天调试程序遇到这么一个警告! Local declaration of 'XXX' hides instance variable 遇到这种原因,是因为本地变量跟函数参数变量同名.改变其一即可.
- UIView的layoutSubviews,initWithFrame,initWithCoder方法
****************************layoutSubviews************************************ layoutSubviews是UIView ...
- 2016-3-6.16:43------------js开始
-------------------------------------------------------------------