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 ...
随机推荐
- 这种方法在受到.NET版本和访问注册表权限时,是最佳解决方案,虽然代码看起来很多,不过下面的类直接拿走用就可以了。
public class FileContentType { private static IDictionary<string, string> _mappings = ne ...
- Ansible6:Playbook简单使用【转】
ansbile-playbook是一系列ansible命令的集合,利用yaml 语言编写.playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特性,它可以允许你传输某个命令 ...
- linux 进程监控和自动重启的简单实现
目的:linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能. 实现原理:由定时任务crontab调用脚本,脚本用ps检查进程是否存在,如果不存在则重 ...
- Explain of Interaction Operators in UML?
来源于:EA 中的 Interaction Operators Enterprise Architect User Guide Operator Action alt Divide up intera ...
- VC2010编译错误
1. cannot convert parameter 1 from 'const char [43]' to 'LPCWSTR' 我是看了这个之后解决问题的~ http://blog.163.com ...
- ios 获得版本号
获取iphone的系统信息使用[UIDevice currentDevice],信息如下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone OS ...
- SQLite错误总结 error code 19: constraint failed
SQLite错误总结 1. android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed错误原 ...
- Loadrunner VuGen实战---基本组成、录制流程、协议、脚本优化、参数化(三)
一.3大基本组件:VuGen.Controller.Analysis 1.VuGen:录制.编写脚本. 2.Controller:性能测试场景设计以及监控的地方. 3.Analysis:生成图表报告的 ...
- tomcat内存优化问题
Java内存组成 1) 堆 运行时数据区域,所有类实例和数组的内存均从此处分配.Java 虚拟机启动时创建.对象的堆内存由称为垃圾回收器 的自动内存管理系统回收. 堆由两部分组成: 其中eden+fr ...
- css 8.1
1. border-collapse 属性设置是否将表格边框折叠为单一边框: table { border-collapse:collapse; } 如果没有规定 !DOCTYPE,border-c ...