1081. Rational Sum (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. PAT1081:Rational Sum

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  2. 1081. Rational Sum (20) -最大公约数

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

  3. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  4. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  5. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

  6. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

  7. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. PAT 1081 Rational Sum

    1081 Rational Sum (20 分)   Given N rational numbers in the form numerator/denominator, you are suppo ...

  9. PAT Rational Sum

    Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...

随机推荐

  1. [HDU1109]模拟退火算法

    模拟退火的基本思想: (1) 初始化:初始温度T(充分大),初始解状态S(是算法迭代的起点),每个T值的迭代次数L (2) 对k=1,……,L做第(3)至第6步: (3) 产生新解$S\prime $ ...

  2. js开发:数组的push()、pop()、shift()和unshift()(转)

    js开发:数组的push().pop().shift()和unshift() 2017-05-18 11:49 1534人阅读 评论(0) 收藏 举报  分类: javascript开发(22)  版 ...

  3. mongodb创建.bat快捷方式

    1.在mongodb安装目录下找到bin文件下的mongod.exe 复制路径,然后创建startmongodb.bat文件 例如 cd C:\Program Files\MongoDB\Server ...

  4. 树莓派 Learning 003 --- GPIO 001 --- 点亮LED

    树莓派 Learning 003 - GPIO 001 - 点亮LED 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 Learni ...

  5. 转:基于InfluxDB&Grafana的JMeter实时性能测试数据的监控和展示

    本文主要讲述如何利用JMeter监听器Backend Listener,配合使用InfluxDB+Grafana展示实时性能测试数据 关于JMeter实时测试数据 JMeter从2.11版本开始,命令 ...

  6. Spring入门第二十七课

    声明式事务 直接上代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dri ...

  7. datatables的使用

    在开发web项目中,界面就是一个以丰富友好的样式来展现数据的窗口,同样的数据不用的展现形式给人不同的体验,数据列表是数据是一种常见展现形式,对于数据列表的一个最基本的要求就是能够实现分页以及检索功能. ...

  8. hdu1062

    #include<stdio.h> #include<string.h> int main() { int i,n,len,j,k,t; char s1]; scanf(&qu ...

  9. MySQL中的正则表达式

    正则表达式是用正则表达式语言来建立 基本字符的匹配 .是正则表达式语言中的一个特殊的字符,它表示匹配任意一个字符 在LIKE和REGEXP之间有一个重要的差别,LIKE匹配整个列,如果被匹配的文本仅在 ...

  10. 前端页面唯一字符串生成(Js)UUID

    function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 3 ...