PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880
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 <bits/stdc++.h>
using namespace std; long long a[111], b[111];
long long sum, m; long long gcd(long long x, long long y) {
long long z = x % y;
while(z) {
x = y;
y = z;
z = x % y;
}
return y;
} long long ad(long long x, long long y) {
if(x > y)
swap(x, y);
if(y % x == 0)
return y;
else
return x * y / gcd(x, y);
} void display(long long p, long long q) {
if(q == 0 || p == 0)
printf("0\n");
else {
bool flag = true;
if(p < 0) {
flag = false;
printf("-");
p = abs(p);
} if(p / q != 0) {
if(p % q == 0)
printf("%lld\n", p / q);
else {
long long mm = p / q;
printf("%lld ", mm);
if(!flag) cout << "-";
printf("%lld/%lld", (p - mm * q) / gcd(p - mm * q, q), q / gcd(p - mm * q, q));
}
} else {
printf("%lld/%lld", p / gcd(p, q), q / gcd(p, q));
} }
} void add(long long x, long long y) {
// sum / m + x / y
// = (sum * y + m * x) / (x * y);
long long xx = sum * y + m * x;
long long yy = m * y;
long long g = gcd(abs(xx), abs(yy));
xx /= g;
yy /= g;
sum = xx;
m = yy;
} int main() { int N;
scanf("%d", &N);
for(int i = 1; i <= N; i ++)
scanf("%lld/%lld", &a[i], &b[i]); if(N == 0) {
printf("0\n");
return 0;
}
if(N ==1) {
display(a[1], b[1]);
return 0;
} /*
long long m = ad(b[1], b[2]);
for(int i = 3; i <= N; i ++) {
m = ad(m, b[i]);
} long long sum = 0;
for(int i = 1; i <= N; i ++) {
sum += a[i] * m / b[i];
}
*/
sum = a[1];
m = b[1];
for(int i = 2; i <= N; i ++) {
add(a[i], b[i]);
} if(m < 0) {
sum = -sum;
m = -m;
}
display(sum, m); return 0;
}
PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)的更多相关文章
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 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 ca ...
随机推荐
- C语言——第一章,1.4程序开发过程
1.4程序开发过程 一,开发过程 1,分析问题,设计一种解决问题的途径(方案)★ 2,写出源代码 (*.c) 3,编译→(连接) *.obj→(*.exe) 4,运行*.exe (可执行程序) 二 ...
- Go Web 使用工具
前端工具: sublime text3 下载:https://www.sublimetext.com/3 中文版设置:https://jingyan.baidu.com/article/9c69d48 ...
- 【bzoj3991】[SDOI2015]寻宝游戏 树链的并+STL-set
题目描述 给出一棵树,初始每个点都是非必经的.多次改变某个点的必经状态,并询问从任意一个点出发,经过所有必经的点并回到该点的最小路程. 输入 第一行,两个整数N.M,其中M为宝物的变动次数. 接下来的 ...
- [agc002D]Stamp Rally-[并查集+整体二分]
Description 题目大意:给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的边的最大编号最小.n, ...
- 【LG3723】[AHOI2017/HNOI2017]礼物
[LG3723][AHOI2017/HNOI2017]礼物 题面 洛谷 题解 首先我们将\(c\)看作一个可以为负的整数,那么我们就可以省去讨论在哪个手环加\(c\)的繁琐步骤了 设我们当前已经选好了 ...
- dotnet core在Task中使用依赖注入的Service/EFContext
C#:在Task中使用依赖注入的Service/EFContext dotnet core时代,依赖注入基本已经成为标配了,这就不多说了. 前几天在做某个功能的时候遇到在Task中使用EF DbCon ...
- cogs87 乘积最大
cogs87 乘积最大 原题链接 题解 竟然不用高精... f[i][j]表示前i位数j个乘号的最大数f[i][j]=max{f[i-l][j-1]*num[i-l+1][i]} num[a][b]表 ...
- DevOps是一种文化,不是角色!
一.DevOps是一种文化,不是角色! 软件无处不在.在如今的世界里,每个主流公司/组织都和软件开发息息相关,并且公司需要向软件一样运作.更快且更敏捷,同时保证安全性和可靠性,这样的要求前所未有的强烈 ...
- HTML中CSS入门基础
HTML.CSS 实用css有三种格式:内嵌:内联:外部: 分类:内联:写在标记的属性位置,优先级最高,重用性最差内嵌:写在页面的head中,优先级第二,重用性一般外部:写在一个以css结尾的文件中, ...
- 获取一个数组里面第K大的元素
如何在O(n)内获取一个数组比如{9, 1, 2, 8, 7, 3, 6, 4, 3, 5, 0, 9, 19, 39, 25, 34, 17, 24, 23, 34, 20}里面第K大的元素呢? 我 ...