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 ...
随机推荐
- Eclipse搭建Android环境失败的解决方案
今天在Eclipse上搭建Android开发环境,不仅在安装ADT的过程中老是出错,而且Android SDK下载后,打开SDK Manager时也无法链接到网页下载tools,网上查了好多方法,试了 ...
- Handler学习小结
在android消息机制中Handler扮演着举足轻重的作用,(AsnyTask其实也是对Handler+Thread做了一层封装),ui线程超过5s就会报出ANR,一般耗时操作操作需要放在子线程中处 ...
- Windows系统版本判定那些事儿
v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...
- PS图像特效算法——百叶窗
这个只要设置好条纹的宽度和条纹的间隔,建立一个遮罩层,等间隔的对原图进行等间距的遮罩. clc; clear all; Image=imread('4.jpg'); Image=double(Imag ...
- Order Management Suite - Pricing and Availability Form Library
In this Document Purpose Scope Details A. Form / Functional Issues "Add to Selection& ...
- 杭电ACM 1004题
原题大概意思就是统计输入字符串中,重复的最大个数! import java.util.Scanner; public class Main { public static void main(Stri ...
- Django之admin的使用和源码剖析
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...
- jQuery之select的option怎样绑定事件
HTML: <select id='select'> <option value='0'>上海</option> <option value='1'>北 ...
- spring是如何管理 事务的
Spring提供的事务管理可以分为两类:编程式的和声明式的.编程式的,比较灵活,但是代码量大,存在重复的代码比较多:声明式的比编程式的更灵活方便. 1.传统使用JDBC的事务管理 以往使用JDBC ...
- happens-before规则和指令重排
...