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. ...
随机推荐
- ecshop3.0.0 release0518 SQL注入
bugscan上的漏洞,自己复现了一下 注入在根目录下的flow.php elseif ($_REQUEST['step'] == 'repurchase') { include_once('incl ...
- extjs经典form的submit()和ajax()
extjs 的submit: // 发送请求 this.formPanel.getForm().submit({ u ...
- 成功抓取douban 所有电影
之前爬了250,想爬所有的电影 Rule(LinkExtractor(allow=(r'https://movie.douban.com/subject/\d+')), callback=" ...
- easyui -grid每列绑定tooltip
/**用法:*/function doCellTip() { $('#dg').datagrid('doCellTip', { 'max-width': '100px' });} /** * 扩展两个 ...
- OracleHelper.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Conf ...
- 【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)
http://www.lydsy.com/JudgeOnline/problem.php?id=3314 一眼就是维护一个距离为d的单调递减队列... 第一次写.....看了下别人的代码... 这一题 ...
- webpack中引入的path[require('path')]是node.js内置的package,用来处理路径的。
http://www.runoob.com/nodejs/nodejs-path-module.html
- MAMP下配置虚拟主机域名
第一步:修改虚拟主机地址: /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf 第二步:
- ext布局问题之tab panel内的gridpanel内容数据变多,出现滚动条
1)解决之道: 1.修改tabPanel var tabs= new Ext.TabPanel({ border: false, region:'center', id:'center', activ ...
- android应用安全——代码安全(android代码混淆)
android2.3的SDK开始在eclipse中支持代码混淆功能(理论上java都支持混淆,但关键在于如何编写proguard的混淆脚本,2.3的SDK使用简单的配置就可以实现混淆).使用SDK2. ...