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 ...
随机推荐
- CentOS7上防火墙操作
firewalld打开关闭防火墙与端口 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl statu ...
- SpringMVC: Ajax技术
SpringMVC:Ajax技术 简介 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种在无需重新加载整个网 ...
- spark on yarn 安装笔记
yarn版本:hadoop2.7.0 spark版本:spark1.4.0 0.前期环境准备: jdk 1.8.0_45 hadoop2.7.0 Apache Maven 3.3.3 1.编译spar ...
- 目标检测评价标准(mAP, 精准度(Precision), 召回率(Recall), 准确率(Accuracy),交除并(IoU))
1. TP , FP , TN , FN定义 TP(True Positive)是正样本预测为正样本的数量,即与Ground truth区域的IoU>=threshold的预测框 FP(Fals ...
- MYSQL 数据库名、表名、字段名查询
//查询所有表的所有字段: select * from information_schema.columns where table_name='sys_users' 效果: //查询指定表的所有字段 ...
- 二十一、CI框架之MCV
一.我们在M模型文件里面添加一个文件,代码如下: 二.在C控制器中加载模型,并调用模型函数,输出达到View,控制器代码如下: 三.在View里面输出控制器传过来的参数 四.显示效果如下: 五.我们对 ...
- jdk8安装
==安装jdk1.8== [root@ycj ~]# mkdir -p /usr/local/src/jdk //创建目录 [root@ycj jdk]# cd /usr/local/src/jdk ...
- Nginx系列p2:重载,热部署,日志分割
今天我们来学习 nginx 的 重载.热部署.日志分割功能 重载:当我们需要修改配置文件中的一些值,我们可以直接修改该配置文件,然后重新启动 nginx 服务,就可以实现在 nginx 不停止服务的情 ...
- Cassandra--Cassandra 安装
当前最新版本:3.11.3 https://cassandra.apache.org/doc/latest/getting_started/installing.html 前提条件 安装Java8. ...
- JS添加、设置属性以及鼠标移入移出事件
源代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...