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 ...
随机推荐
- 【POJ】2492 A Bug's Life
题目链接:http://poj.org/problem?id=2492 题意:给你n个虫子,m组实验.让你帮科学家找一下有没有虫子是同性恋. 题解:假设x是一个性别,x+n为另一个性别.如果在同性的集 ...
- "_CMTimeGetSeconds", referenced from:
CMTime is defined in the CoreMedia.framework. Add that framework to your project.
- vc面试题目
class B { public: B() { cout << "default constructor" << endl; } ~B() { cout & ...
- linux 将子文件夹的文件复制到 当前目录中
linux 将子文件夹的文件复制到 当前目录中,如 目录结构大概是 -sh |__ db_backup |___ test |____ 2018_01_01_00_00_00 |_____ 2018_ ...
- Sqlite && EF Code FIRST 终极解决方案 2019.5.17
Sqlite && EF Code FIRST 终极解决方案 2019.5.17 包括根据模型自动生成数据库,初始化数据,模型改变时的自动数据迁移等 2019.12.25 更新 支持E ...
- thinkphp 跳转和重定向
页面跳转 在应用开发中,经常会遇到一些带有提示信息的跳转页面,例如操作成功或者操作错误页面,并且自动跳转到另外一个目标页面.系统的\Think\Controller类内置了两个跳转方法success和 ...
- qfc 问题汇总(TO BE CONTINUED)
硬件问题 增加一个复位按钮 程序问题 /* uart allocation: PB6-7: UART1 -> TELEM PD5-6 : UART2 -> SBUS PD8-9: UART ...
- UVA-699-The Falling Leaves-二叉树+递归
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
- Daemon 守护线程(27-11)
t2.setDaemon(True)不再等待里面的sleep(5). 当设成setDaemon(True)这个线程就不等了. 例子一: import threadingfrom time import ...
- centos 7 开机优化shell
vim start_init.sh #!/bin/bash#####dns echo "nameserver 114.114.114.114" >/etc/resolv.c ...