pat1081. Rational Sum (20)
1081. Rational Sum (20)
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.
Input Specification:
Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number, then the sign must appear in front of the numerator.
Output Specification:
For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.
Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Sample Output 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24
测试数据比较弱。如果要算:(2^63)/(3)+(1)/(5) 怎么办??
#include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==)
{
return a;
}
return gcd(b,a%b);
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n,i;
long long fz,ffz,fm,ffm,com;
while(scanf("%d",&n)!=EOF)
{
scanf("%lld/%lld",&fz,&fm);
com=gcd(fz,fm);
fz/=com;
fm/=com;
for(i=; i<n; i++)
{
//cout<<"i: "<<i<<endl;
scanf("%lld/%lld",&ffz,&ffm);
com=gcd(fm,ffm);
//cout<<"com: "<<com<<endl;
ffz=ffz*(fm/com);
//cout<<"ffz: "<<ffz<<endl;
fm=fm*(ffm/com);
//cout<<"fm: "<<fm<<endl;
fz=fz*(ffm/com);
//cout<<"fz: "<<ffm<<endl;
fz+=ffz;
//cout<<"fz: "<<fz<<endl;
com=gcd(fz,fm);
//cout<<"com: "<<com<<endl;
fz/=com;
//cout<<"fz: "<<fz<<endl;
fm/=com;
//cout<<"fm: "<<fm<<endl;
} //cout<<fz<<" "<<fm<<endl; if(fz%fm==) //可以整除
{
printf("%lld\n",fz/fm);
}
else
{
if(fz/fm>)
{
printf("%lld ",fz/fm);
}
printf("%lld/%lld\n",fz-fz/fm*fm,fm);
}
}
return ;
}
pat1081. Rational Sum (20)的更多相关文章
- PAT1081:Rational Sum
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- 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 ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- 1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT Rational Sum
Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...
随机推荐
- java ----一个函数传回多个值的总结
java 一个函数如何返回多个值 参考方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值的key是什么,只能通过doc或者通过源代码来查看. 2.传入一个引用进去,修改引用的属性值.问 ...
- 自定义滚动条jQuery插件- Perfect Scrollbar
主要特性: 不需要修改任何的元素的css 滚动条不影响最初的页面布局设计 滚动条支持完整的自定义 滚动条的尺寸和位置会随着容器尺寸或者内容的变化而变化 依赖于jQuery和相关几个类库 不需要定义宽度 ...
- WPF实现右键菜单
ContextMenu类就是用来做右键菜单的对象,对于任何的控件都可以进行对ContextMenu属性的操作进行设置右键菜单的功能. 下面代码就是对一个按钮添加一个WPF右键菜单的功能: < B ...
- CentOS 配置RDP
XRDP服务器 CentOS安装XRDP实现远程桌面访问: 由于安装的是远程桌面,因此需要安装桌面显示服务:# yum install vnc-server 下面开始配置XRDP服务 l 配置环境: ...
- Project Web Server PSI 接口一些常用操作
对Project Web Server进行二次开发,每天都把自己折腾到12点以后才休息,到处都是坑,研究那些烦人的PSI,国内根本查不到PSI相关的资料,对照API文档一点点谷歌资料,全部英文资料,开 ...
- WSGI 简介(使用python描述)
WSGI 简介 背景 Python Web 开发中,服务端程序可以分为两个部分,一是服务器程序,二是应用程序.前者负责把客户端请求接收,整理,后者负责具体的逻辑处理.为了方便应用程序的开发,我们把常用 ...
- Blast 如何使用Blast+(Linux)转载
下载数据 #此处下载对应物种的数据库ftp://ftp.ncbi.nih.gov/genomes/,下载fna格式的即可 wget ftp://ftp.ncbi.nih.gov/genomes/A ...
- 主元素问题(Java)
x称为一个长度为n的数组的a的主元素,如果这个数组里面等于x的元素的数目不少于n/2个. 例如,a={2,3,2,2,5,3,2,4,2},x=2就是这个主元素.给定包含n个元素的数组a,主元素问题就 ...
- SAS笔记(7) PROC SQL
参考资料:<Longitudinal Data and SAS: A Programmer's Guide>
- AOP常用注解
1.@Aspect 配置切面Bean,和<bean.../>元素进行配置无区别,一样支持依赖注入来配置属性值: 如果启动了Spring的"零配置"特性,一样可以让Spr ...