https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880

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 <bits/stdc++.h>
using namespace std; long long a[111], b[111];
long long sum, m; long long gcd(long long x, long long y) {
long long z = x % y;
while(z) {
x = y;
y = z;
z = x % y;
}
return y;
} long long ad(long long x, long long y) {
if(x > y)
swap(x, y);
if(y % x == 0)
return y;
else
return x * y / gcd(x, y);
} void display(long long p, long long q) {
if(q == 0 || p == 0)
printf("0\n");
else {
bool flag = true;
if(p < 0) {
flag = false;
printf("-");
p = abs(p);
} if(p / q != 0) {
if(p % q == 0)
printf("%lld\n", p / q);
else {
long long mm = p / q;
printf("%lld ", mm);
if(!flag) cout << "-";
printf("%lld/%lld", (p - mm * q) / gcd(p - mm * q, q), q / gcd(p - mm * q, q));
}
} else {
printf("%lld/%lld", p / gcd(p, q), q / gcd(p, q));
} }
} void add(long long x, long long y) {
// sum / m + x / y
// = (sum * y + m * x) / (x * y);
long long xx = sum * y + m * x;
long long yy = m * y;
long long g = gcd(abs(xx), abs(yy));
xx /= g;
yy /= g;
sum = xx;
m = yy;
} int main() { int N;
scanf("%d", &N);
for(int i = 1; i <= N; i ++)
scanf("%lld/%lld", &a[i], &b[i]); if(N == 0) {
printf("0\n");
return 0;
}
if(N ==1) {
display(a[1], b[1]);
return 0;
} /*
long long m = ad(b[1], b[2]);
for(int i = 3; i <= N; i ++) {
m = ad(m, b[i]);
} long long sum = 0;
for(int i = 1; i <= N; i ++) {
sum += a[i] * m / b[i];
}
*/
sum = a[1];
m = b[1];
for(int i = 2; i <= N; i ++) {
add(a[i], b[i]);
} if(m < 0) {
sum = -sum;
m = -m;
}
display(sum, m); return 0;
}

  

PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)的更多相关文章

  1. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

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

  2. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  3. PAT 1081 Rational Sum

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

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

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

  5. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

  6. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  7. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

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

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

随机推荐

  1. Python学习笔记十:json序列化,软件结构目录规范,ATM作业讲解,import本质论

    json序列化 将系统的某个状态保存为字符串(挂起),序列化. import json json.dumps():序列化 json.loads():反序列化 简单类型数据处理 import pickl ...

  2. ACM1009:FatMouse' Trade

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  3. CALL TRANSACTION

    概要 SUBMITと違い.トランザクションコードで呼び出すのが特徴. アドオンからの伝票照会やバッチインプットによるSAPへのデータ登録/更新処理にも利用される. なお.呼び出された側から呼び出し元へ ...

  4. 20155234 实验三 敏捷开发与XP实践

    20155234 实验三 敏捷开发与XP实践 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验步骤 (一)敏捷开发与XP 敏捷开发(Agile Development)是一种以人为核心.迭 ...

  5. pwd命令的实现

    pwd 命令描述 Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd ...

  6. 20155301 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    20155301 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 步骤:打开cmd,建立 ...

  7. 20155305 2016-2017-2 《Java程序设计》 实验五 Java网络编程及安全实验报告

    20155305 2016-2017-2 <Java程序设计> 实验五 Java网络编程及安全实验报告 实验内容 1.掌握Socket程序的编写. 2.掌握密码技术的使用. 3.设计安全传 ...

  8. Dlib库中实现正脸人脸检测的测试代码

    Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下: #i ...

  9. 【LG1445】樱花

    [LG1445]樱花 题面 洛谷 题解 \[ \frac 1x+\frac 1y=\frac 1{n!}\\ \frac{x+y}{xy}=\frac 1{n!}\\ n!(x+y)=xy\\ xy- ...

  10. 最优布线问题(wire.cpp)

    最优布线问题(wire.cpp) [问题描述] 学校有n台计算机,为了方便数据传输,现要将它们用数据线连接起来.两台计算机被连接是指它们间有数据线连接.由于计算机所处的位置不同,因此不同的两台计算机的 ...