PAT Advanced 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
题目分析
给出N有理数,格式为分子/分母,若为负,则负号一定在分子前。求N个有理数的和
解题思路
- 输入分子、分母,化简
- 计算累加分数和与下一个分数和,化简
- 累加完成后,假分数转换为真分数,打印整数部分和分式部分
易错点
- 若和为0,要输出“0”(否则测试点4错误)
知识点
- long long类型的数据输入/输出
输入:scanf("%lld",&n);
输出:printf("%lld",n);
Code
Code 01
#include <iostream>
using namespace std;
// 求公约数
int gcd(long long a, long long b) {
return b==0?abs(a):gcd(b, a%b);
}
// 化简分式
void reduction(long long &a, long long &b) {
int gcdvalue = gcd(a,b);
a /= gcdvalue;
b /= gcdvalue;
}
int main(int argc,char * argv[]) {
long long n,a,b,suma=0,sumb=1,gcdvalue;
scanf("%lld",&n);
for(int i=0; i<n; i++) {
scanf("%lld/%lld",&a,&b);
reduction(a,b);
suma=a*sumb+suma*b;
sumb=b*sumb;
reduction(suma,sumb);
}
long long itg = suma/sumb;
suma = suma-(sumb*itg);
if(itg!=0) {
printf("%lld",itg);
if(suma!=0)printf(" ");
}
if(suma!=0)printf("%lld/%lld",suma,sumb);
if(itg==0&&suma==0)printf("0");
return 0;
}
PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]的更多相关文章
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- 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 ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]
题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...
- PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...
随机推荐
- 五十九、SAP中常用预定义系统变量
一.SAP中常用预定义系统变量 内容如下: 二.系统变量定义在结构SYST里,我们打开SE38 三.在代码编辑器输入SYST变量 四.双击SYST,来到这个系统结构,里面有很多系统变量 五.我们随便写 ...
- ActiveMQ消息队列和SignalR之日志实时监控及警报小实例
主要技术: log4net-生成日志. ActiveMQ-生成日志的时候发送消息,并实时监控日志. SignalR-将ActiveMQ监控的日志实时显示到浏览器上,而不用刷新浏览器. 小实例介绍: 左 ...
- 实验吧-web-天下武功唯快不破(Python中byte和str的转换)
题目:看看响应头 打开网站,既然已经提示我们看响应头了,那我们就看看呗(习惯bp,也可直接F12查看) 可以看到,响应头部分有个FLAG,而且有提示:please post what you find ...
- HDU - 4112 Break the Chocolate(规律)
题意:有一块n*m*k的巧克力,最终需要切成n*m*k个1*1*1的块,问用以下两种方法最少掰多少次能达到目的: 1.用手掰:每次只能拿出一块来掰:2.用刀切:可以把很多已经分开的块摞在一起一刀切下来 ...
- javascript设计模式(1)——面向对象基础
用对象收编变量2种方式 1 函数式 var Object = { name:function(){ return this; }, email:function(){ return this; } } ...
- JS常用的正则表达式包
结构: Code: /* 用途:检查输入的Email信箱格式是否正确 输入:strEmail:字符串 返回:如果通过验证返回true,否则返回false */ function checkEmail( ...
- 二十二、CI框架之模型别名
一.在控制器中调用模型时,可以给模型取别名,之后调用时,调用别名就可以了 二.界面显示如下: 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477 ...
- javascript 连续赋值(连等运算)问题研究
前几天看到一个javascript 连续赋值的问题,运行了一下,结果出乎意料,发现这里的水真的有点深啊,连续赋值的底层机制,没有一本前端书籍有详细介绍的,自己做实验研究了一下,先来看结果: var a ...
- DevOps元数据管理
元数据是自动化运维的基础,对元数据的管理和查询贯穿整个运维的生命周期.我们从一个元数据的使用场景开始: 双十一抢购火热进行中,某电商后端实例的日志中出现了502错误码,运维平台监测到该异常并发送告警给 ...
- Redis高级用法
第一章 redis初识 1.1 Redis是什么 介绍 开源:早起版本2w3千行 基于键值对的存储系统:字典形式 多种数据结构:字符串,hash,列表,集合,有序集合 高性能,功能丰富 那些公司在用 ...