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. ...
随机推荐
- Win7下 OpenCV+Qt开发环境搭建
1.所需软件工具: (1)OpenCV开发库,2.4.9版:包括源文件(source文件夹)和编译后的文件(build文件夹),但最好自己使用CMake又一次编译.否则easy出错. (2)Qt Cr ...
- C#中的事件介绍
什么是事件?事件有哪些?怎么用事件? 一.什么是事件? 事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动.输入值改变等等,或者是一些出现,如系统生成的通知.应用程序需要在事件发生时响 ...
- Zookeeper中的选举机制
Zookeeper虽然在配置文件中并没有指定master和slave,但是,zookeeper工作时,是有一个节点为leader,其他则为follower.leader是通过内部的选举机制临时产生的. ...
- xaf 学习 RuleUniqueValueAttribute 唯一验证。
xaf 学习 RuleUniqueValueAttribute 唯一验证. RuleUniqueValue("", DefaultContexts.Save, CriteriaE ...
- 用阿里云搭建Http代理服务器
先说下我的运行环境: Ubuntu16.04+python3.5,用的是阿里云ECS乞丐版. 搭建步骤: [python] view plain copy 0. 直接用xshell或putty远程到云 ...
- python通过日志分析加入黑名单
监控nginx日志,若有人攻击,则加入黑名单,操作步骤如下:1.读取日志文件2.分隔文件,取出ip3.将取出的ip放入list,然后判读ip的次数4.若超过设定的次数,则加入黑名单 日志信息如下: 1 ...
- 学习笔记:Vue+Node+Mongodb 构建简单商城系统(二)
前面几个月工作有点忙,导致构建简单商城系统的计划搁置近三个月.现在终于有时间重新回过头来继续本计划.本篇主要记录自己在阿里云服务器上搭建node运行环境的整个过程,以及对其中遇到的一些问题的思考. 一 ...
- js 代码优化 (写的可以)
Javascript是一门非常灵活的语言,我们可以随心所欲的书写各种风格的代码,不同风格的代码也必然也会导致执行效率的差异,开发过程中零零散散地接触到许多提高代码性能的方法,整理一下平时比较常见并且容 ...
- 【matlab】生成列是0-255渐变的图像
图像大小:640×512 8位灰度图 %% 生成图像 %大小:* %类型:灰度图 %灰度值:列按照0-255渐变,故命名为column shade. clc,clear all,close all; ...
- MANIFEST.MF 文件内容完全详解
http://blog.csdn.net/zhifeiyu2008/article/details/8829637