PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
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 (数据不严谨 点名批评)的更多相关文章
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 1081. Rational Sum (20)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
随机推荐
- unix文件共享
UNIX系统支持在不同的进程间共享打开文件.内核使用3种数据结构表示打开文件,他们之间的关系决定了在文件共享方面一个进程对另一个进程产生的影响. (1)每个进程在进程表中都有一个记录项,记录项中包含一 ...
- Scala数组操作
数组操作 初始化固定长度的数组 // 初始化长度为10的数组 val array = new Array[Int](10) // 初始化创建含有hello与Scala的数组 val s = Array ...
- 2014年第五届蓝桥杯B组(C/C++)预赛题目及个人答案(欢迎指正)
参考:https://blog.csdn.net/qq_30076791/article/details/50573512 第3题: #include<bits/stdc++.h> usi ...
- 20155203 2016-2017-2 《Java程序设计》第10周学习总结
20155203 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程(Java Socket编程) Java最初是作为网络编程语言出现的,其对网络提供 ...
- 20155234java实验一
实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习Linux基础入门(新版))Vim ...
- 同步备份工具之 rsync
1.常用同步方法 SCP. NFS. SFTP. http. samba. rsync. drbd(基于文件系统同步,效率高) 2.rsync 介绍 rsync,英文全称是 remote synchr ...
- C#:设置CefSharp的一些参数,比如忽略安全证书
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本次记录如何设置CefSharp忽略安全证书,以及他的一些其他配置 参考网址: https://peter.s ...
- Retinex图像增强和暗通道去雾的关系及其在hdr色调恢复上的应用
很多人都认为retinex和暗通道去雾是八杆子都打不着的增强算法.的确,二者的理论.计算方法都完全迥异,本人直接从二者的公式入手来简单说明一下,有些部分全凭臆想,不对之处大家一起讨论. 首先,为描述方 ...
- Intellij IDEA 2017 通过scala工程运行wordcount
首先是安装scala插件,可以通过idea内置的自动安装方式进行,也可以手动下载可用的插件包之后再通过idea导入. scala插件安装完成之后,新建scala项目,右侧使用默认的sbt 点击Next ...
- javaweb(三十一)——国际化(i18n)
一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...