Source:

PAT A1081 Rational Sum (20 分)

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 where integeris 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的更多相关文章

  1. PAT1081:Rational Sum

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  2. PAT 1081 Rational Sum

    1081 Rational Sum (20 分)   Given N rational numbers in the form numerator/denominator, you are suppo ...

  3. PAT Rational Sum

    Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...

  4. PAT 1081 Rational Sum[分子求和][比较]

    1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...

  5. pat1081. Rational Sum (20)

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  6. 1081. Rational Sum (20) -最大公约数

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

  7. A1081. Rational Sum

    Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...

  8. 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 ...

  9. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...

随机推荐

  1. JS实现的ajax

    function createXMLHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { try { return new ...

  2. VTemplate模板引擎的使用--进阶篇

    1.<vt:template>与<vt:include>标签的不同 <vt:template>和<vt:include> 标签都包含file属性,如果这 ...

  3. 使用soapui进行webservice接口测试

    一.web service(SOAP)与HTTP接口的区别   1.什么是web service WebService就是Web服务的意思,对应的应用层协议为SOAP(相当于HTTP协议),可理解为远 ...

  4. PAT甲级——A1152 GoogleRecruitment【20】

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  5. testNG官方文档翻译-5 测试方法,测试类和测试组

    5.1 - 测试方法 测试方法是被 @Test注解的方法.这些方法的返回值会被忽略,除非在testng.xml中将allow-return-values设置为true. <suite allow ...

  6. C语言函数指针用法

    #include <stdio.h> #include <string.h> static void sayHello(); static void salute(); voi ...

  7. Linux使用yum install 安装程序时,提示“另外一个程序锁定了 yum;等待它退出……”

    Linux使用yum install 安装程序时,提示“另外一个程序锁定了 yum:等待它退出……” 原因: yum命令一次只能安装一个软件,所以当你下载安装第二个软件包时,系统进程锁会锁定yum,这 ...

  8. Lock中使用Condition实现等待通知

    Condition类有很好的灵活性,可以实现多路通知功能,一个Lock对象中可以创建多个Condition对象实例,线程对象可以注册在指定的Condition中,进而有选择的进行线程通知,在调度线程上 ...

  9. C语言各种进制输出

    #include<stdio.h> int main() { ; float f = 12.0; printf("十进制输出:%d\n", number); print ...

  10. Android开发环境部署:JDK+Android Studio

    1. 刚开始接触Android开发,首先需要为你的电脑安装java JDK(Java开发工具包),不管是用Eclipse还是Android Studio都需要只吃Java语言运行吧. 官网:Oracl ...