PAT Rational Sum
Rational Sum (20)
题目描述
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.
输入描述:
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.
输出描述:
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.
输入例子:
5
2/5 4/15 1/30 -2/60 8/3
输出例子:
3 1/3
#include<iostream>
#include<cstring>
#include<string>
using namespace std; long long gcd(long long x,long long y){
return y?gcd(y,x%y):x;
} long long lcm(long long x,long long y){
return x/gcd(x,y)*y;
} char s[]; int main(){
long long a=,b=; //a / b
int n;
cin >> n;
while(n--){
cin >> s;
char *t = strstr(s,"/");
if(t) *t = ' '; // a/b + c/d
long long c,d;
sscanf(s,"%lld %lld",&c,&d);
long long aa = a*d + b*c;
long long bb = b*d;
long long g = gcd((aa<)?(-aa):aa,bb);
a = aa/g;
b = bb/g;
}
long long x = a/b,y = a%b;
if(y == ) printf("%lld\n",x);
else{
if(x) printf("%lld ",x);
printf("%lld/%lld\n",y,b);
}
return ;
}
PAT Rational 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_A1081#Rational Sum
Source: PAT A1081 Rational Sum (20 分) Description: Given N rational numbers in the form numerator/de ...
- PAT1081:Rational Sum
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- pat1081. Rational Sum (20)
1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...
- PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]
题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...
随机推荐
- 【Hadoop 分布式部署 六:环境问题解决和集群基准测试】
环境问题: 出现Temporary failure in name resolutionp-senior-zuoyan.com 的原因有很多,主要就是主机没有解析到, 那就在hadoop的sl ...
- Docker2之Service
Make sure you have published the friendlyhello image you created by pushing it to a registry. We’ll ...
- CentOS7时间和日期的同步 (chrony和)
CentOS 6版本,使用 hwclock CentOS 7版本,使用timedatectl 1.基本概念 1.1 GMT,UTC,CST,DST时间 世界标准时间 整个地球分为二十四时区,每个时区都 ...
- Json序列化提示缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor”
//缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor” namespace System.Runtime.Compi ...
- WebStorm破解方法
http://www.jianshu.com/p/85266fa16639 http://idea.lanyus.com/ webstorm 入门指南 破解方法 1. 下载的WebStorm http ...
- MySQL 并发测试中,线程数和数据库连接池的实验
我一直以来,对性能测试中,连接池的大小要如何配置,不是太清楚: 就我所知道的,就DB自带对连接数的限制,在sqlserver中用select @@connection 可以查到, 在代码中,可以配置D ...
- Java java.lang.SecurityException: Prohibited package name
java.lang.SecurityException: Prohibited package name 提示java错误: Exception in thread "main" ...
- laydate控制之前的日期不可选择
laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填 }); 只要加一个min参数,就可以控制了.0表示之前的日期 ...
- Hibernate的查询功能
1.Query对象 1.使用Query对象,不需要写sql语句,但是写hql语句 (1)hql:hibernate query language,提供查询语言,这个hql语言和普通sql语句相似 (2 ...
- 学习笔记30—Windows那些事
1.win10编程窗口:powerShell 2.Win7设置工具栏折叠:我们只需要在底部任务栏空白位置点击鼠标右键,然后选择“属性”,在弹出额属性对话框中,将“任务栏按钮”后面的“始终合并.隐藏标签 ...