PAT_A1081#Rational Sum
Source:
Description:
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
whereinteger
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
Keys:
Code:
/*
Data: 2019-07-05 19:37:00
Problem: PAT_A1081#Rational Sum
AC: 26:24 题目大意:
给N个分数,求和
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M = 1e3;
struct fr
{
long long up;
long long down;
}temp; int gcd(int a, int b)
{
if(b==) return a;
else return gcd(b,a%b);
} fr Reduction(fr s)
{
if(s.up == )
s.down = ;
else
{
int d = gcd(abs(s.up), s.down);
s.up /= d;
s.down /= d;
}
return s;
} fr Add(fr s1, fr s2)
{
fr s;
s.up = s1.up*s2.down+s2.up*s1.down;
s.down = s1.down*s2.down;
return Reduction(s);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d\n", &n);
fr ans = fr{,};
for(int i=; i<n; i++)
{
scanf("%lld/%lld", &temp.up, &temp.down);
ans = Add(ans, temp);
} if(ans.down==)
printf("%lld\n", ans.up);
else if(ans.up >= ans.down)
printf("%lld %lld/%lld\n", ans.up/ans.down, abs(ans.up)%ans.down, ans.down);
else
printf("%lld/%lld\n", ans.up, ans.down); return ;
}
PAT_A1081#Rational Sum的更多相关文章
- PAT1081:Rational Sum
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- 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 ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- A1081. Rational Sum
Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
随机推荐
- uuid 去中心化的唯一性
A Universally Unique IDentifier (UUID) URN Namespace https://tools.ietf.org/html/rfc4122.html A UUID ...
- PHP-模拟请求和操作响应
模拟请求 fsockopen <?php // 建立连接 $link = fsockopen('localhost', '80'); define('CRLF', "\r\n" ...
- 46、tensorflow入门初步,手写识别0,1,2,3,4,5,6
1.使用tensorflow的SoftMax函数,对手写数字进行识别 Administrator@SuperComputer MINGW64 ~ $ docker run -it -p 8888:88 ...
- linux命令行光标移动技巧
看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下面工作.但是事情也不是那么简单,还是需要知道“如何去做” ...
- Problem opening .cshtml files
Hi Spartai, Welcome to MSDN forum. What is the version of your Visual Studio? It`s works fine for me ...
- 力扣算法题—148sort-list
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- PHP数组循环遍历的几种方式
PHP数组循环遍历 1.for循环 <?php //语法 for (init counter; test counter; increment counter) { code to be exe ...
- css篇-简化版
[CSS篇]简化版 (1) CSS盒模型 CSS盒模型 题目:谈谈你对CSS盒模型的认识 1) 基本概念:标准模型+IE模型 2) 标准模型和IE模型的区别 计算宽度和 ...
- MOV EAX,DWORD PTR SS:[EBP+8]
nasm来写可以写成mov eax,dword ptr [ebp + 8]理由:ebp和esp默认是ss段,所以根本不用显式说明. eax,ebx,ecx,edx,edi,esi默认 ...
- 归并排序(Merge_Sort)
基本思想 建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用. 算法原理 归并操作指的是将两个已经排序的序列合并成一个序列的操作,归并 ...