当前位置:网站首页>592. Fraction addition and subtraction
592. Fraction addition and subtraction
2022-07-27 13:58:00 【anieoo】
Original link :592. Fractional addition and subtraction
simulation :
class Solution {
public:
// Find the greatest common divisor by division
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
string fractionAddition(string expression) {
int n = 0;
for(auto &x :expression) {
if(x == '/') n++;
}
expression = '+' + expression;
int a = 0,b = 1,offset = 0;
int c, d;
char e;
for (int i = 0; i < n; i ++ ) {
sscanf(expression.c_str() + offset, "%c%d/%d", &e, &c, &d);
offset += (e + to_string(c) + '/' + to_string(d)).size();
if (e == '-') c = -c;
int x = a * d + b * c, y = b * d;
int z = gcd(x, y);
a = x / z, b = y / z;
}
if(b < 0) b = -b, a = -a;
return to_string(a) + '/' + to_string(b);
}
};
边栏推荐
- Thinkphp+ pagoda operation environment realizes scheduled tasks
- Cesium region clipping, local rendering
- 【每日一题】1206. 设计跳表
- Recommended collection, confusing knowledge points of PMP challenge (2)
- 小程序毕设作品之微信校园洗衣小程序毕业设计成品(2)小程序功能
- Huiliang technology app is a good place to go to sea: after more than ten years of popularity, why should the United States still choose to go to sea for gold
- JS 模块、闭包应用
- 纯c手写线程池
- 13、用户web层服务(一)
- Structural thinking
猜你喜欢

SQL tutorial: introduction to SQL aggregate functions

小程序毕设作品之微信校园洗衣小程序毕业设计成品(7)中期检查报告

egg-swagger-doc 图形验证码解决方案

opencv图像的缩放平移及旋转

Small program completion work wechat campus laundry small program graduation design finished product (2) small program function

致尚科技IPO过会:年营收6亿 应收账款账面价值2.7亿

Gray histogram

在“元宇宙空间”UTONMOS将打开虚实结合的数字世界

Conditions and procedures of futures account opening

Wechat campus laundry applet graduation design finished product of applet completion work (8) graduation design thesis template
随机推荐
13、用户web层服务(一)
Travel notes from July 11 to August 1, 2022
List map basic notes
Add index to the field of existing data (Damon database version)
小程序毕设作品之微信校园洗衣小程序毕业设计成品(1)开发概要
WPF visifire.charts4.6.1 tutorial with source code
Thinkphp+ pagoda operation environment realizes scheduled tasks
Application layer World Wide Web WWW
Sword finger offer II 041. Average value of sliding window
Is it still time to take the PMP Exam in September?
[training day4] anticipating [expected DP]
认知篇----硬件工程师的成才之路之经典
idea Gradle7.0+ :Could not find method compile()
Dat.gui control custom placement and dragging
Charles tutorial
SQL教程之 SQL 聚合函数入门教程
将目标检测大尺寸图片裁剪成固定尺寸图片
[luogu_p4556] [Vani has an appointment] tail in rainy days / [template] segment tree merging
Network packet loss, network delay? This artifact helps you get everything done!
Selenium eight elements positioning and relative locator