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 ...
随机推荐
- 109-PHP类成员初始化值
<?php class mao{ //定义猫类 public $age=0; //定义多个属性并初始化 public $weight=50; public $color='white'; } $ ...
- PyCharm下创建并运行我们的第一个Django项目
PyCharm下创建并运行我们的第一个Django项目 准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm 1. 创建一个新的工程 第一次运行Pycha ...
- 第一部分 JavaScript语言核心(三)
第六章 对象 P123 在ES3中,点运算符后的标识符不能是保留字.如果一个对象的属性名是保留字,name必须使用方括号的形式访问它们,如o["for"]和o["clas ...
- windows driver 获取本地时间
#define ArrayLength 260 void MyGetLocalTime() { LARGE_INTEGER li_system; LARGE_INTEGER li_Local; cha ...
- Java算法练习——正则表达式匹配
题目链接 题目描述 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素 所谓匹配,是要 ...
- 9 —— node —— 读取文件及文件夹的名字
const fs = require('fs'); fs.readdir('./','utf8',(err,data)=>{ console.log(data) })
- 大数据高可用集群环境安装与配置(04)——安装JAVA运行环境
Hadoop运行在java环境,所以在安装Hadoop之前,需要安装好jdk 提前下载好jdk安装包(jdk-8u161-linux-x64.tar.gz),将它上传到指定的安装目录当中,然后运行安装 ...
- Java语言学习总结 扩展篇 DateFormat类
DateFormat类 java.text .DateFormat 是 日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换:也就是可以在Date对象与String对象之间进 ...
- 【新年呈献】高性能网络通信框架 HP-Socket v5.7.1
项目主页 : http://www.oschina.net/p/hp-socket 开发文档 : https://www.docin.com/p-2287339564.html 下载地址 : http ...
- Eclipse 快速打开文件所在的本地目录
目前收集到两种方法: 1.快捷键:Ctrl+Shift+W 2.利用Eclipse的External Tools设置快捷方式 第二种方法步骤: a.Run->External Tools-> ...