PAT甲级——A1081 Rational Sum
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 (≤), 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 <vector>
#include <math.h>
using namespace std;
long int gcd(long int a, long int b)
{
if(b==) return a;
else return gcd(b,a%b);
}
int main()
{
int N;
cin >> N;
long long Inter = , resa = , resb = , a, b, Div, Mul;
for (int i = ; i < N; ++i)
{
char c;
cin >> a >> c >> b;
resa = resa * b + a * resb;//同分相加的分子
resb = resb * b;
Inter += resa / resb;//简化
resa = resa - resb * (resa / resb);
Div = gcd(resb, resa);
resa /= Div;
resb /= Div;
}
if (Inter == && resa == )
cout << << endl;
else if (Inter != && resa == )
cout << Inter << endl;
else if (Inter == && resa != )
cout << resa << "/" << resb << endl;
else
cout << Inter << " " << resa << "/" << resb << endl;
return ;
}
PAT甲级——A1081 Rational Sum的更多相关文章
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- A1081. Rational Sum
Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- 【PAT甲级】1104 Sum of Number Segments (20 分)
题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- PAT_A1081#Rational Sum
Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT Rational Sum
Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...
随机推荐
- PostgreSQL 主键自动增长
建立主键并设置自动增加的办法好好几种,这里记录我测试过的: drop table pro_process; CREATE TABLE "public"."pro_proc ...
- mysql中重复数据只取条
select * from table_a where id in (select min(id) from table_a group by a) )) SUBSTRING_INDEX(cids,' ...
- wish - 简单的窗口式(windowing) shell
总览 wish [filename] [arg] [arg ...] 选项 -colormap new 指定窗口使用一个新的私有的调色板(colormap)而不使用给屏幕的缺省的调色板. -displ ...
- IDEA 打开Run Dashboard 分组启动
一,项目文件夹中,找到 .idea-->workspace.xml 添加: <component name="RunDashboard"> <option ...
- elast数据存放
这几天一直在索引数据,突然发现服务器状态变红色了,去官网看了下 集群状态如果是红色的话表示有数据已经丢失了!这下头大了才索引了7G数据,后面还有10多个G, 我在liunx下看了下磁盘空间 发现运行e ...
- python读文件判断是否已到EOF
python读文件判断是否已到EOF,也即结尾,一般其它语言都是以EOF直接来判断的,比如 if ( fp.read(chunk_size) == EOF), 但python到结尾后是返回空字符串的, ...
- [JZOJ3690] 【CF418D】Big Problems for Organizers
题目 题目大意 给你一棵树,然后有一堆询问,每次给出两个点. 问所有点到两个点中最近点的距离的最大值. 正解 本来打了倍增,然后爆了,也懒得调-- 显然可以在两个点之间的路径的中点处割开,一边归一个点 ...
- CSS——元素的显示与隐藏
元素的显示与隐藏 在CSS中有三个显示和隐藏的单词比较常见,我们要区分开,他们分别是 display visibility 和 overflow. 他们的主要目的是让一个元素在页面中消失,但是不在文档 ...
- thinkphp url大小写
系统默认的规范是根据URL里面的模块名.控制器名来定位到具体的控制器类的,从而执行控制器类的操作方法. 以URL访问 http://serverName/index.php/Home/Index/in ...
- JSONObject 序列化后,对象数据为引用地址
最近在json序列化的时候,遇到了个坑,记录如下: public static void main(String[] args) { JSONObject json = new JSONObject( ...