PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目
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
题目分析
给出N有理数,格式为分子/分母,若为负,则负号一定在分子前。求N个有理数的和
解题思路
- 输入分子、分母,化简
- 计算累加分数和与下一个分数和,化简
- 累加完成后,假分数转换为真分数,打印整数部分和分式部分
易错点
- 若和为0,要输出“0”(否则测试点4错误)
知识点
- long long类型的数据输入/输出
输入:scanf("%lld",&n);
输出:printf("%lld",n);
Code
Code 01
#include <iostream>
using namespace std;
// 求公约数
int gcd(long long a, long long b) {
return b==0?abs(a):gcd(b, a%b);
}
// 化简分式
void reduction(long long &a, long long &b) {
int gcdvalue = gcd(a,b);
a /= gcdvalue;
b /= gcdvalue;
}
int main(int argc,char * argv[]) {
long long n,a,b,suma=0,sumb=1,gcdvalue;
scanf("%lld",&n);
for(int i=0; i<n; i++) {
scanf("%lld/%lld",&a,&b);
reduction(a,b);
suma=a*sumb+suma*b;
sumb=b*sumb;
reduction(suma,sumb);
}
long long itg = suma/sumb;
suma = suma-(sumb*itg);
if(itg!=0) {
printf("%lld",itg);
if(suma!=0)printf(" ");
}
if(suma!=0)printf("%lld/%lld",suma,sumb);
if(itg==0&&suma==0)printf("0");
return 0;
}
PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]的更多相关文章
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT (Advanced Level) 1081. Rational Sum (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- 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 ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]
题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...
- PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...
随机推荐
- 十七、SAP中使用SQL语句读取一条数据
一.需要说明的是SAP不同类型的结构体类型之间是不能随意赋值的,如果需要赋值,可以使用CORRESPONDING FIELDS OF关键字, 不同类型结构体中同名的成员会被赋值,代码如下: 二.输出代 ...
- 《新标准C++程序设计》3.8(C++学习笔记10)
友元 友元分为友元函数和友元类两种. 一.友元函数 在定义一个类的时候,可以把一些函数(包括全局函数和其它类的成员函数)声明为“友元”,这样那些函数就成为该类的友元函数,在友元函数内部就可以访问该类对 ...
- SciKit-Learn 使用matplotlib可视化数据
章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Learn 可视化数据:主成分分析(P ...
- 【LeetCode】509. 斐波那契数
题目 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 F(N) = ...
- Linux每日一练20200220
- Spring入门之一-------实现一个简单的IoC
一.场景模拟 public interface Human { public void goHome(); } Human:人类,下班了该回家啦 public interface Car { void ...
- cf 758D - Ability To Convert
从后往前贪心就好了.各种各样0的情况太BT了.. (各种爆long long,f**k) #include<bits/stdc++.h> #define LL long long #def ...
- CocoaPods安装/卸载/初始化等常用操作
CocoaPods的官网:https://cocoapods.org/,官方指导文档https://guides.cocoapods.org/ 1)ruby gem源更换国内源gems.ruby-ch ...
- 吴裕雄--天生自然Django框架开发笔记:Django 表单
HTML表单是网站交互性的经典方式. 用Django对用户提交的表单数据进行处理. HTTP 请求 HTTP协议以"请求-回复"的方式工作.客户发送请求时,可以在请求中附加数据.服 ...
- php和js的小区别
1.今天看了下php的api感觉还可以,不是很难,可能没看到深入的地方, (1)和js很相似 目前感觉它和js的最大区别 js的 点 被替换成 -> function setCate($pa ...