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. 深入浅出MFC学习笔记 第三章 MFC六大关键技术之仿真

    0:MFC类层次结构 1:MFC程序的初始化过程CWinApp::InitApplication()CMyWinApp::InitInstance()CMyFrameWnd::CMyFrameWnd( ...

  2. 20155202 实验四 Android开发基础

    20155202 实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握An ...

  3. 20155212 2016-2017-2 《Java程序设计》第3周学习总结

    20155212 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 Chapter 4 要产生对象必须先定义类,类是对象的设计图,对象时类的实例. 一个原始码中 ...

  4. 20155227 《Java程序设计》实验五 Java网络编程及安全实验报告

    20155227 <Java程序设计>实验五 Java网络编程及安全实验报告 实验内容 任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能. 编写MyDC.java实现从上面 ...

  5. 20155328 2016-2017-2 《Java程序设计》 第十周学习内容总结

    20155328 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 JAVA和ANDROID开发学习指南 第22章 网络概览 两台计算机用于通信的语言叫做&qu ...

  6. oracle-11g-64位安装和plaql

    1.oracle卸载 如果是新装,请跳过此步骤 卸载步骤: 1.停止所有服务 2.用自带删除软件,删除所有目录 3.打开注册表: -->运行regedit,删除HKEY_LOCAL_MACHIN ...

  7. slqite3练习

    连接 import sqlite3 con = sqlite3.connect(":memory:") c = con.cursor() # Create table c.exec ...

  8. centos7下将java -jar命令运行一个项目做成systemd服务

    有些时候运行一个java项目在linux下通过一条简单的java命令即可,如: #nohup java -jar jenkins.war & ###这里为后台运行jenkins 在此背景下,j ...

  9. QT-2D编程

    QT-[转]2D编程 Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕上和绘图·设备上进行绘制,主要基于QPainter.QPainterDevice和QPainterEngine这3个类. ...

  10. [POI2011]MET-Meteors

    题面 题解 首先我们尝试暴力,那么就对每个点二分一下即可. 我们发现单独二分复杂度太高,而且有些地方很浪费,如求前缀和等. 那么我们就想,能否将它们合并在一起二分呢? 于是就有了整体二分 整体二分即可 ...