PAT1081:Rational Sum
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 思路 分子相加的运算。
1.辗转相除法求分子分母的最大公约数
2.两分数相加后要化简,不然容易在计算时产生溢出。
3.输出需要特别注意的格式:
1)在整数不为0的情况下,分数为0则只输出整数。
2)在整数为0的情况下,分数不为0则只输出分数。
3)二者都为0直接输出一个0。
4)二者都不为0按题目要求的标准格式输出。
4.关于分母为0的情况,题目测试用例好像并未考虑,暂不做处理。 代码
#include<iostream>
using namespace std;
typedef long long ll; ll gcd(ll a,ll b) //求最大公约数
{
return b == 0?abs(a):gcd(b,a % b);
}
int main()
{
ll N,a,b,gvalue,suma,sumb;
while( cin >> N)
{
suma = 0,sumb = 1;
for(int i = 0;i < N;i++)
{
scanf("%lld/%lld",&a,&b);
gvalue = gcd(a,b);
//约分
a /= gvalue;
b /= gvalue;
//分数求公倍数相加
suma = a * sumb + b * suma;
sumb = b * sumb;
//分子和约分
gvalue = gcd(suma,sumb);
suma /= gvalue;
sumb /= gvalue;
}
ll integer = suma / sumb;
ll numerator = suma - integer * sumb;
if(integer != 0)
{
cout << integer;
if(numerator != 0)
{
cout << " ";
printf("%lld/%lld",numerator,sumb);
}
}
else
{
if(numerator != 0)
{
printf("%lld/%lld",numerator,sumb);
}
else
cout << 0;
}
cout << endl;
}
}
PAT1081:Rational Sum的更多相关文章
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- PAT 1081 Rational Sum
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppo ...
- PAT Rational Sum
Rational Sum (20) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Given N ration ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- PAT_A1081#Rational Sum
Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...
- 1081. Rational Sum (20) -最大公约数
题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...
- A1081. Rational Sum
Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
随机推荐
- OC语言(三)
十九.一些规范 import系统自带的用尖括号<>来包含. 发现需求不清晰,一定要先搞明白才去做. 多文件开发,文件名和类名一致 命令行里的做法:(只是编译链接主文件,但是全部编译链接会出 ...
- Microsoft Dynamics CRM 2011 JS操作集锦
1.Xrm.Page.context 用户ID:getUserId() 用户角色:getUserRoles() 用户语言:getUserLcid() 组织名称:getOrgUniqueName() 组 ...
- Erlang Rebar 使用指南之四:依赖管理
Erlang Rebar 使用指南之四:依赖管理 全文目录: https://github.com/rebar/rebar/wiki 本章链接: https://github.com/rebar/re ...
- 尽量用pass-by-reference-to-const(const引用)替换pass-by-value(传值)
默认情况下C++以pass-by-value传递对象至函数(或从函数返回). eg1: class Person { public: Person(); virtual ~Person(); priv ...
- LDA实现
topic model本质上就一个套路,在doc-word user-url user-doc等关系中增加topic层,扩充为2层结构,一方面可以降维,另一方面挖掘深层次的关系,用户doc word ...
- Order&Shipping Transactions Status Summary
Order&Shipping Transactions Status Summary Step Order Header Status Order Line Status Order Flow ...
- 集群RPC通信
RPC即远程过程调用,它的提出旨在消除通信细节.屏蔽繁杂且易错的底层网络通信操作,像调用本地服务一般地调用远程服务,让业务开发者更多关注业务开发而不必考虑网络.硬件.系统的异构复杂环境. 先看看集群中 ...
- CRF资料
与最大熵模型相似,条件随机场(Conditional random fields,CRFs)是一种机器学习模型,在自然语言处理的许多领域(如词性标注.中文分词.命名实体识别等)都有比较好的应用效果.条 ...
- Http的会话跟踪和跨站攻击(xss)
会话跟踪 什么是会话? 客户端打开与服务器的连接发出请求到服务器响应客户端请求的全过程称之为会话. 什么是会话跟踪? 会话跟踪指的是对同一个用户对服务器的连续的请求和接受响应的监视. 为什么需要会话跟 ...
- cocos2dx 跨平台编译遇到的几个问题
首先声明一下自己用的版本 vs2010 cocos2d-x_2.2 ndk_r9 1. 安装cygwin之后,也设置好了 ndk_root, 但是 cd $NDK_ROOT 进入不了, 只好 ...