Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.

给定N个有理数,分子/分母,要求你计算它们的和

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.

输入规格:每个输入文件包含一个测试建立,每个测试案例,第一个是N,紧接着N个有理数据,a1/b1,a2/b2,…所有的分子和分母都是长整型,如果是负数,就必须出现在分子的前面

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.

针对每个测试案例,输出他们的和,数字,分子/分母 数字时候和的整数部分,分子<分母 比你高且分子和分母没有相同的因子,如果整数部分为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

思路核心

学会最大公约数,最小公倍数,模拟出分数加和通分的过程。

完整代码

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll; //记住ll为long long
int gcd(ll a,ll b){//求a和b最大公约数if(b==0) return a;else return gcd(b,a%b);
}
struct Fraction{ll up,down;
};Fraction reduction(Fraction result){if(result.down<0){result.up = -result.up;result.down = -result.down;}if(result.up == 0){result.down = 1;}else{int d = gcd(abs(result.up),abs(result.down));result.up /= d;result.down /= d;}return result;
}
Fraction add(Fraction f1,Fraction f2){Fraction res;res.up = f1.up*f2.down + f2.up*f1.down;res.down = f1.down * f2.down;return reduction(res);
}
void showResult(Fraction r){reduction(r);if(r.down == 1) printf("%lld\n",r.up);else if(abs(r.up)>r.down){printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);}else{printf("%lld/%lld\n",r.up,r.down);}
}
int main()
{//freopen("C:\\Users\\Administrator\\Desktop\\test\\input.txt","r",stdin);Fraction res{0,1};int n;scanf("%d",&n);for(int i =0;i<n;i++){Fraction tmp;scanf("%lld/%lld",&tmp.up,&tmp.down);res = add(res,tmp);}showResult(res);return 0;
}

方法2,手动模拟分数

#include<iostream>
using namespace std;
typedef long long ll;
ll a[100],b[100],sigmal[100];ll gy(ll x,ll y){ll big,small;if(x>y){big = x;small = y;}else{big = y;small = x;}while(small!=0){long tmp = big%small;big = small;small = tmp;}return big;
}ll gbs(ll x,ll y){ll tmp = gy(x,y);return x*(y/tmp);
}int main()
{// freopen("C:\\Users\\Administrator\\Desktop\\test\\input.txt","r",stdin);ll i,j,k,l,N;scanf("%lld",&N);for(i = 0;i<N;i++){scanf("%lld/%lld",&a[i],&b[i]);if(a[i]<0){a[i]*=-1;sigmal[i]=-1;}elsesigmal[i] = 1;}ll A,B;B = b[0];for(i = 1;i<N;i++){B = gbs(B,b[i]);}A= 0;for(i = 0;i<N;i++){A+= sigmal[i] * a[i]*(B/b[i]);}//分子累加ll tmp = gy(A,B);A/=tmp;B/=tmp;if(A==0){cout << 0;return 0;}if(A<0){cout << "-";A*=-1;}if(A>=B){cout << A/B;}if(A>=B&&A%B){cout << " ";}if(A%B){cout << A%B << "/" << B;}return 0;
}

C++题解1081 Ration相关推荐

  1. PAT甲级1081 Rational Sum:[C++题解]分数求和、辗转相除法求最大公约数、long long有一个数据溢出

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析 数据量很小,直接模拟分数加法即可,分数加法如下: ab+cd=ad+bcbd\frac{a}{b}+\frac{c}{d}=\frac ...

  2. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

  3. 【最新合集】PAT乙级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! pat乙级题解目录 编号 标题 题解 分类 1001 害死人不偿命的(3n+1)猜想 (15分) 9行代码AC 水 1002 写出这 ...

  4. 牛客题霸 车站建造问题 C++题解/答案

    题目描述 有108个村庄排在一条公路上,依次编号为0~108-1,相邻村庄距离为1,其中有n个村庄居住着牛牛,居住着牛牛的村庄从小到大依次为a0~an-1,其中保证a0=0. 现在需要建设车站,有两个 ...

  5. [Codevs] 1081 线段树练习 2 ----“分块!”

    1081 线段树练习 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所 ...

  6. 【题解】PAT (Basic Level) Practice (中文)

    互联网行业的小白,写博客的目的是为了记录自己的学习过程.对自己学习中所犯的错误做一个总结.由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教! PAT Basic Level P ...

  7. PTA乙级题解(110题全)

    First of all 如果你是你们学校的ACM选手 那么我猜你的目标可能是下面几个吧(^v^) (1) ACM区域赛拿牌 (2) PTA顶级(Top Level) (3) CCF-CSP认证400 ...

  8. PAT乙级全题解存档

    1001 害死人不偿命的(3n+1)猜想 #include <iostream> using namespace std;int main() {int n, cnt = 0;cin &g ...

  9. 信息学奥赛一本通 1081:分苹果 | OpenJudge NOI 小学奥数 7826:分苹果

    [题目链接] ybt 1081:分苹果 OpenJudge NOI 小学奥数 7826:分苹果 [题目考点] 1. 求和 [解题思路] 该问题可以抽象为:n个整数,都大于0,n个数不同,求这n个数的和 ...

最新文章

  1. 两句话动态修改table数据并提交到后台
  2. sys模块 和os模块
  3. 使用ASP生成HTML文件
  4. [html] 当img标签中的src图片加载失败时,怎么让它变得更美观呢?
  5. MySQL_管理与维护
  6. 常用的Oracle命令整理
  7. 华为防火墙配置(L2TP)
  8. 【Power BI ---M语言】M语言基础一
  9. 微信小程序data format error解决办法
  10. sf授权php,授权系统全解源码(支持分子系统)【原完整版】
  11. 手机消息推送方案综述
  12. 淮安万达机器人_淮安万博机器人 万达上班时间【输入网址YB7888.vip】angmi_PP视频搜索-PP视频-原PPTV聚力视频...
  13. Windows凭据管理器
  14. 图形学笔记(四) 数学变换
  15. 在线教育如何做好直播?
  16. 数字电路课设_电子抢答器
  17. websocket自动重连
  18. python语言可以处理数据文件吗_Python语言读取Marc后处理文件基础知识.pdf
  19. Android自定义Dialog
  20. QT基础入门【调试篇】QT程序如何打包发布生成可执行exe文件(win下的可执行程序)

热门文章

  1. EditText自定义光标的问题
  2. 假设银行一年整存零取的月息为0.63%
  3. java 枚举参数为对象_Java枚举值
  4. 逐行向上滚动 类似中奖名单的小例子
  5. python运行不显示结果图示_pycharm运行和调试不显示结果的解决方法
  6. PhotoScape X Pro for Mac(简单易用的照片编辑器)
  7. android个人理财通项目_公告|MNSC项目周报(2019.03.11—2019.03.20)
  8. 博客园主页样式修改(加透明背景和微调位置)
  9. 如何克隆一个虚拟机/如何把虚拟机克隆一份给别人用/虚拟机互相通信
  10. python时间日期格式,python日期时间格式化