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. ...
随机推荐
- MySQL5.7的配置文件
5.7 /etc/mysql/mysql.conf.d/mysqld.cnf 5.6 /etc/my.cnf 或 /etc/mysql/my.cnf
- python+fastcgi+flup获取请求的数据和环境变量
关于nginx+python+fastcgi+flup搭建fastcgi环境没问题了,可是如何处理POST/GET的请求数据呢,这个问题着实纠结了没久,通过尝试和阅读flup的源代码,终于明白了.先上 ...
- UVALive 7721 K - 2-ME Set 集合dp,所有数的位或来表示状态。
/** 题目:UVALive 7721 K - 2-ME Set 链接:https://vjudge.net/problem/UVALive-7721 题意:给定n个数,从中取出一个集合,至少包含两个 ...
- mysql—Access denied for user 'root'@'localhost' (using password:NO)
安装mysql未设置初始密码,登录提示Access denied for user 'root'@'localhost' (using password:NO): 解决方案: sudo /etc/i ...
- ubuntu 14.04 中安装R和Rstudio
1. 安装R 1.1 首先添加镜像源 sudo gedit /etc/apt/sources.list # 加入新镜像源: deb http://cran.rstudio.com/bin/linux/ ...
- 【python】map list for 运行时长测试
import time,sys reps=1000 size=10000 def tester(func,*args): starttime=time.time() for i in range(re ...
- Python入门(六):标准库
操作系统接口 os模块提供了不少与操作系统相关联的函数. import os os.getcwd() # 返回当前的工作目录 os.chdir('d:/') # 修改当前的工作目录 os.system ...
- 总结 一下UML 类图的关系
1,实线三角 表示 泛化 是一种继承关系,它指定了子类如何特化父类的所有特征和行为 2,虚线三角 表示 实现 是一种类与接口的关系,表示类是接口所有特征和行为的实现 3,空心菱形 表示 聚合 是整 ...
- Eclipse UML插件
Green UML http://green.sourceforge.net/ AmaterasUML http://amateras.sourceforge.jp/cgi-bin/fswiki_en ...
- Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授
本文转载至 http://blog.csdn.net/woaifen3344/article/details/41311023 Code3000极光推送erroryour certificate n ...