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 ...
随机推荐
- Quartz.NET+TopSelf 实现定时服务
转载http://www.cnblogs.com/jys509/p/4628926.html Quartz.NET 入门 2015-07-09 00:59 by jiangys, 67858 阅读, ...
- WPF Invoke与BeginInvoke的区别
Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托. Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句 ...
- 洛谷-关押罪犯-NOIP2010提高组复赛
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
- wdcp安全设置,让你的后台,只有你自己能访问
wdcp安全设置,让你的后台,只有你自己能访问 wdcp的后台,默认端口,是8080,可以修改为其它端口wdcp的后台,可以限制IP地址的访问,也可以限制域名的访问做了这些限制与设置后,已相对安全了, ...
- shell工具使用配置备忘
一.bash之vi mode.两种方式:set -o vi(只让bash自己进入vi模式)或 set editing-mode vi(让所有使用readline库函数的程序在读取命令行时都进入vi模式 ...
- hdu1051
#include<iostream> #include<algorithm> using namespace std; struct SIZE { int l; int w; ...
- Mac效率工具推荐
1.Homebrew 命令行安装神器 https://brew.sh/index_zh-cn.html 2.Alfred 类似spotlight,功能更强大 https://www.alfredapp ...
- ibatis知识点汇总
一个参数,返回Map <select id="getShopInfo" parameterClass="java.lang.String" resultC ...
- LeetCode: 453 Minimum Moves to Equal Array Elements(easy)
题目: Given a non-empty integer array of size n, find the minimum number of moves required to make all ...
- java排序算法(持续更新)
package exception; import java.util.Arrays; public class Sort { public static void main(String[] arg ...