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 ...
随机推荐
- MTP 写字机器
目标 无意中看到下面视频,我打算也实现一个类似机器 视频.视频2.视频3 来源于油管Creativity Buzz的创意,顺便了解到有家AxiDraw公司在生产这种机器,淘宝上也有售卖. 想法 观看视 ...
- python写web服务器
#coding = utf-8 from http.server import BaseHTTPRequestHandler, HTTPServer class RequestHandler(Base ...
- File操作-将数据库里的数据写入到指定路径的txt文件里
package com.Cristin.File;//将数据库里的数据写入到指定路径的txt文件里 import java.io.File;import java.io.FileOutputStrea ...
- Vue--vux组件库
各种组件demo源码~ https://doc.vux.li/zh-CN/
- _itemmod_currency_like
设置物品掉落模式为货币类型功能:掉落的时候 所有人都可以拿,就像公正徽章,每个人都会获得一个.小技巧:配合DBC使用,可以将该道具其显示在角色栏的货币中.1.转存item_template,在item ...
- 正则获取 某段 DIV 中 的内容
string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...
- leecode第一百三十六题(只出现一次的数字)
class Solution { public: int singleNumber(vector<int>& nums) { int len=nums.size(); ; ;i&l ...
- JqueryValidate 表单验证插件
1.适用场景 表单 ( 支持自定义规则 ) 2.相关文章 jQuery Validate 3.实际问题 JqueryValidate表单相同Name不校验问题解决
- Linux中apt与apt-get命令的区别与解释
2019-01-15 14:35:39 随着 apt install package 命令的使用频率和普遍性逐步超过 apt-get install package,越来越多的其它 Linux 发行版 ...
- ssh服务及安全配置
1.清空防火墙 关闭 setenforcesetenforce 2 getenforce 3 setenforce 0 4 iptables -F 5 systemctl stop firewal ...