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 ...
随机推荐
- UDK编辑器 49条小提示
转自:http://www.cnblogs.com/hmxp8/archive/2012/02/09/2343674.html Very Helpful~ 01. First time using t ...
- cadence spb 16.5 破解过程实例和使用感受_赤松子耶_新浪博客
cadence spb 16.5 破解过程实例和使用感受_赤松子耶_新浪博客 Cadence Allegro16.5详细安装具体的步骤 1.下载SPB16.5下来后,点setup.exe,先安装第一项 ...
- Sequence Models 笔记(二)
2 Natural Language Processing & Word Embeddings 2.1 Word Representation(单词表达) vocabulary,每个单词可以使 ...
- 特征降维之SVD分解
奇异值分解.特征值分解是一个提取矩阵特征很不错的方法,但是它只是对方阵而言的,在现实的世界中,我们看到的大部分矩阵都不是方阵,比如说有N个学生,每个学生有M科成绩,这样形成的一个N * M的矩阵就不可 ...
- mongodb-help功能
mongo-help功能 version:2.6.12下面是示例: > help db.help() help on db methods db.m ...
- sharepoint SDDL 字符串包含无效的SID或无法转换的SID
安装过程中出现以下错误 采用独立模式安装Sharepoint Server 2013/Foundation 2013,在进行配置向导的时候会碰到这样的错误 System.ArgumentExcepti ...
- CountDownLatch分析
1 什么是CountDownLatch呢? 先看看官网的定义 :一种同步帮助,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成. 现在由我来解释什么是CountDownLatch吧:比如说我 ...
- TortoiseSVN 日常操作指南
TortoiseSVN A Subversion client for Windows Stefan Küng Lübbe Onken Simon Large 2005/01/17 19:09:21 ...
- Animations使用01 BrowserAnimationsModule
1 基础知识 1.1 动画 就是从一个状态过渡到另外一个状态 1.2 状态 元素本身的形状.颜色.大小等 1.3 Angular中的动画 给元素添加一个触发器,当这个触发器被触发后就会让该元素的状态发 ...
- SpringSecurity04 利用JPA和SpringSecurity实现前后端分离的认证和授权
1 环境搭建 1.1 环境说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 SpringSecurity:5.0.7 IDEA:2017.02旗舰版 1.2 环境搭建 创建一 ...