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 ...
随机推荐
- python操作sql server2008 pyodbc
使用Python通过PyODBC连接数据的注意事项 今天使者用PyODBC连接数据库,试了很久才出来,现把一些心得体会和大家分享! 一.PyODBC的下载地址: http://code.google. ...
- C语言学习笔记--C语言中变量的属性关键字
变量属性关键字的使用语法:property type var_name; 1.auto 关键字 auto关键字是C语言中局部变量的默认的关键字,C编译器默认所有的局部变量都是auto的,它表明了被修饰 ...
- How to Write a Spelling Corrector用java 写拼写检查器 Java实现 以备查验
import java.io.*;import java.util.*;import java.util.regex.*; class Spelling { private final HashMap ...
- Windchill 预览效果偏向左边
文档预览效果偏左 解决方法: 1.修改worker配置,去掉“fit worksheet to a single page”的勾 2.进行services,重新启动以下服务 3.重启windchill ...
- css 雪碧图
CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问 该页面时,载入的图片就不会像以前那样一幅一幅地 ...
- 5.SSRF服务器端请求伪造
SSRF(服务端请求伪造):是一种由攻击者构造形成由服务端发起请求的一个安全漏洞. 一般情况下,SSRF攻击的目标是从外网无法访问的内部系统.(正是因为它是由服务端发起的,所以它能够请求到与它相连而与 ...
- 4. DVWA亲测暴力破解
LOW等级 我们先用burpsuite抓包,因为burpsuite提供了暴力破解模块 我们先创建一个1.txt文件夹,把正确的账号密码写进去 我们输入 Username:1 Password: ...
- .Net Core WebApi返回日期格式的问题
环境:.net core 2.1 webapi 问题简介: 返回DateTime,前端接收到的字符有时候为2018-01-01T12:01:01,有时候为2018-01-01T01:01:01.722 ...
- 《OD学spark》20160924scala基础
拓展: Hadoop 3.0 NameNode HA NameNode是Active NameNode是Standby可以有多个 HBase Cluster 单节点故障? HBaster -> ...
- android 拖拽图片&拖动浮动按钮到处跑
来自老外: 拖拽图片效果 方法一: 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLa ...