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 ...
 
随机推荐
- 柱体内温度分布图 MATLAB
			
对于下底面和侧面绝热,上底面温度与半径平方成正比的柱体,绘制柱体内温度分布图. 这里给出两种尝试:1.散点图:2.切片云图 1. 散点图仿真 首先使用解析算法求的场解值的解析表达,其次求解Bessel ...
 - node.js 监听message事件  message字符串丢失信息
			
const dgram = require("dgram"); const server = dgram.createSocket("udp4"); serve ...
 - 20155222 2016-2017-2 《Java程序设计》第9周学习总结
			
20155222 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,开发人员使用JDBC的标 ...
 - 20155307 2017-2018-2 《Java程序设计》第2周学习总结
			
20155307 2017-2018-2 <Java程序设计>第2周学习总结 教材学习内容总结 整数:short(2 byte).int(4 byte).long(8 byte) 字节(b ...
 - 20155310 2016-2017-2 《Java程序设计》第四周学习总结
			
20155310 2016-2017-2 <Java程序设计>第四周学习总结 一周两章新知识的自学与理解真的是很考验和锻炼我们,也对前面几章我们的学习进行了检测,遇到忘记和不懂的知识就再复 ...
 - 20145226夏艺华 《Java程序设计》实验报告四
			
实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试 了解Android组件.布局管理器的使用 掌握Android中事件处理机制 Andro ...
 - [转]理解Linux文件系统之inode
			
很少转发别人的文章,但是这篇写的太好了. 理解inode 作者: 阮一峰 inode是一个重要概念,是理解Unix/Linux文件系统和硬盘储存的基础. 我觉得,理解inode,不仅有助于提高系统 ...
 - Javascript库,前端框架(UI框架),模板引擎
			
JavaScript库:JQuery,undoscore,Zepto 纯Javascript语言封装, 前端框架(UI框架):Bootstrap,Foundation,Semantic UI,Pure ...
 - 内容安全策略(CSP)
			
内容安全策略(CSP),其核心思想十分简单:网站通过发送一个 CSP 头部,来告诉浏览器什么是被授权执行的与什么是需要被禁止的.其被誉为专门为解决XSS攻击而生的神器. 1.CSP是什么 CSP指的是 ...
 - Python不生成HTMLTestRunner报告-转载学习
			
1.问题:Python中同一个.py文件中同时用unittest框架和HtmlReport框架后,HtmlReport不被执行. 2.为什么?其实不是HtmlReport不被执行,也不是HtmlRep ...