PAT 1081 Rational Sum[分子求和][比较]
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
(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
题目大意:给出几个分数求和。
//又是分数求和的题目,细节会很多啊。
#include <iostream>
#include <map>
using namespace std; int n;
int nums[]; int getSum(int s,int f){
if(f>=n){//如果已经到了尽头,那么就对s约分并且输出
//还是需要先根据分母找出所有的最小公倍数呢?
//一点也不会。写不下去。
}
} int main() { cin>>n;
for(int i=;i<n;i++){
cin>>nums[i];
}
cout<<getSum(,); return ;
}
代码转自:https://www.liuchuo.net/archives/2108
#include <iostream>
#include <cstdlib>
using namespace std;
long long gcd(long long a, long long b) {return b == ? abs(a) : gcd(b, a % b);}
int main() {
long long n, a, b, suma = , sumb = , gcdvalue;//注意数据类型的定义。
scanf("%lld", &n);
for(int i = ; i < n; i++) {
scanf("%lld/%lld", &a, &b);
gcdvalue = gcd(a, b);//找到最大公约数进行约分。
a = a / gcdvalue;
b = b / gcdvalue;
suma = a * sumb + suma * b;//分子
sumb = b * sumb;//计算分母
gcdvalue = gcd(suma, sumb);
sumb = sumb / gcdvalue;//再约分。
suma = suma / gcdvalue;
}
long long integer = suma / sumb;
suma = suma - (sumb * integer);//计算余下的分子。
if(integer != ) {
printf("%lld", integer);
if(suma != ) printf(" ");//如果余下的分子不为0.
}
if(suma != )
printf("%lld/%lld", suma, sumb);
if(integer == && suma == )
printf("");
return ;
}
//学习了,需要经常复习吧,要不还是不会。
PAT 1081 Rational Sum[分子求和][比较]的更多相关文章
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- 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 (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- 1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...
- 1081 Rational Sum(20 分)
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
随机推荐
- The Definitive Guide To Django 2 学习笔记(二) 第二个View 动态内容
“Hello World”只是简单的展现了Django 基本的工作机制,但它不是动态的网页.第二个View我们将创建一个动态的网页,该页面上将展现当前的时间和日期. 该View需要做两件事,第一,计算 ...
- 数据库设计(二)Introduction to Database Design
原文链接:http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html Introduction to Databa ...
- win 7和虚拟机之间ping
虚拟机的网卡和真实机的IP设为同一网段.用ping检查是否通,通了后用工具连接ssh看能否登录!如果成功的话winscp应该就能用了.如果不同就要就检查防火墙看是否开放了22端口,没有的话就要开放一下 ...
- 使用command对象添加删除查询记录
private void button1_Click(object sender, EventArgs e) { //实例化数据库连接对象 SqlConnection sqlcon = new Sql ...
- Oracle(2)数据库
1.使用"||"连接多个字段,合并成一列 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFudGluZ21laQ==/font/5a ...
- eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法
使用eclipse JavaEE 版,新建 Dynamic Web Project 项目.在项目里添加 JSP 文件,会在文件头部出现错误提示.提示语句为:The superclass "j ...
- 基于nginx的token认证
Nginx 的 token 认证是基于集成了 nginx+lua 的 openresty 来实现的. 环境: centos 7 部署方式: 增量部署(不影响原 nginx 版本) 版本: openre ...
- 第二百四十节,Bootstrap巨幕页头缩略图和警告框组件
Bootstrap巨幕页头缩略图和警告框组件 学习要点: 1.巨幕组件 2.页头组件 3.缩略图组件 4.警告框组件 本节课我们主要学习一下 Bootstrap 的四个组件功能:巨幕组件.页头组件.缩 ...
- 【BZOJ】3203: [Sdoi2013]保护出题人(几何+三分+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3203 wa无数次QAQ,犯sb错....一是数组没有引用...二是输出转成了int(越界了sad). ...
- CentOS 同步时间
来源:http://www.ctusky.com/16/0497/ 用date查看系统当前时间,date -R 可查看时区. CentOS 同步时间由ntp服务提供,可以用"yum inst ...