团体程序设计天梯赛-练习集 L1-009 分数加法模板
来源:互联网 发布:spark sql ppt 编辑:程序博客网 时间:2024/05/05 09:13
#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;int n;char buff[32];ll gcd(ll a, ll b) { if (b==0) return a; return gcd(b, a%b);}struct frac { ll up, down; frac(ll _up=0, ll _down=1) { up=_up; down=_down; if (up&down) { ll temp=gcd(abs(up), abs(down)); up/=temp; down/=temp; } } friend frac operator+(const frac &f1, const frac &f2) { frac res; ll lcm=f1.down/gcd(abs(f1.down), abs(f2.down))*f2.down; res.up=f1.up*(lcm/f1.down)+f2.up*(lcm/f2.down); res.down=lcm; if (res.up&res.down) { ll temp=gcd(abs(res.up), abs(res.down)); res.up/=temp; res.down/=temp; } return res; } friend frac operator+=(const frac &f1, const frac &f2) { return f1+f2; }};int main(){ //freopen("in.txt", "r", stdin); scanf("%d", &n); frac res; for (int i=0; i<n; i++) { scanf("%s", buff); ll up, down; sscanf(buff, "%lld/%lld", &up, &down); res=res+frac(up, down); } if (res.up==0) puts("0"); else if (abs(res.up)<abs(res.down)) printf("%lld/%lld\n", res.up, res.down); else { if (res.up%res.down==0) printf("%lld\n", res.up/res.down); else printf("%lld %lld/%lld\n", res.up/res.down, res.up%res.down, res.down); } return 0;}
0 0
- 团体程序设计天梯赛-练习集 L1-009 分数加法模板
- 团体程序设计天梯赛-练习集 L1
- 团体程序设计天梯赛-练习集L1-001. Hello World
- 团体程序设计天梯赛-练习集L1-002. 打印沙漏
- 团体程序设计天梯赛-练习集L1-003. 个位数统计
- 团体程序设计天梯赛-练习集L1-004. 计算摄氏温度
- 团体程序设计天梯赛-练习集L1-006. 连续因子
- 团体程序设计天梯赛-练习集L1-007. 念数字
- 团体程序设计天梯赛-练习集L1-010. 比较大小
- 团体程序设计天梯赛-练习集L1-011. A-B
- 团体程序设计天梯赛-练习集L1-012. 计算指数
- 团体程序设计天梯赛-练习集L1-014. 简单题
- 团体程序设计天梯赛-练习集L1-016. 查验身份证
- 团体程序设计天梯赛-练习集L1-017. 到底有多二
- 团体程序设计天梯赛-练习集L1-018. 大笨钟
- 团体程序设计天梯赛-练习集L1-019. 谁先倒
- 团体程序设计天梯赛-练习集L1-020. 帅到没朋友
- 团体程序设计天梯赛-练习集L1-022. 奇偶分家
- Vi编辑异常退出解决
- 软件经理基本素质
- myeclipse清理缓存方法(图解)
- Android沉浸式状态栏
- Vim 插件配置
- 团体程序设计天梯赛-练习集 L1-009 分数加法模板
- SWI 软中断详解
- 自定义 Switch 开关按钮
- House Robber
- hellowiiiiii!(๑•ั็ω•็ั๑) 我要开始记录我的成长啦!!!
- C#获取时间戳的方法
- 支持多QQ登录的软件
- C#工厂模式代码实例
- 当REST遇上Java