当前位置:网站首页>592. 分数加减运算
592. 分数加减运算
2022-07-27 12:54:00 【anieoo】
原题链接:592. 分数加减运算
模拟:
class Solution {
public:
//辗转相除法求最大公约数
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);
}
};
边栏推荐
- 基于STM32的自由度云台运动姿态控制系统
- The finished product of wechat campus laundry applet graduation design (1) development outline
- Small program completion work wechat campus laundry small program graduation design finished product (2) small program function
- Motion attitude control system of DOF pan tilt based on stm32
- Construction and application of industrial knowledge atlas (2): representation and modeling of commodity knowledge
- Crop the large size image of target detection into a fixed size image
- [internship experience] add your own implementation method to the date tool class
- List map basic notes
- 【图论】负环
- Brief tutorial for soft exam system architecture designer | system design
猜你喜欢

期货公司开户后续会有哪些服务?

井贤栋等蚂蚁集团高管不再担任阿里合伙人 确保独立决策

建议收藏,PMP应战篇(2)之易混淆知识点

Motion attitude control system of DOF pan tilt based on stm32

redis集群搭建-使用docker快速搭建一个测试redis集群

For.. of can be used to traverse which data

Data enhancement in image processing

使用RecyclerView,实现列表左滑菜单

In the "meta cosmic space", utonmos will open the digital world of the combination of virtual and real
![[C Advanced] pointer array vs array pointer](/img/1e/33f9cc9446dcad8cdb78babbb5a22c.jpg)
[C Advanced] pointer array vs array pointer
随机推荐
【2022-07-25】
小程序毕设作品之微信校园洗衣小程序毕业设计成品(3)后台功能
Echart line chart displays the last point and vertical dotted line by default
Keras deep learning practice - recommend system data coding
使用RecyclerView,实现列表左滑菜单
redis集群搭建-使用docker快速搭建一个测试redis集群
NoSQL —— NoSQL 三大理论基石 —— CAP —— BASE—— 最终一致性
What services will the futures company provide after opening an account?
期货公司开户后续会有哪些服务?
Leetcode error reporting and its solution
The most complete collection of strategies! Super AI painting tool midjourney open beta! Come and build your fantasy metauniverse
Browser kernel module composition
关于max做动画的一些关键信息(shift+v)
基于STM32的自由度云台运动姿态控制系统
Install the wireless network card driver
利用C语言实现URL解析的基本方法之优秀
[internship experience] add your own implementation method to the date tool class
CARLA 笔记(04)— Client 和 World (创建 Client、连接 World 、批处理对象、设置 Weather、设置 Lights、World snapshots)
Accuracy improvement method: efficient visual transformer framework of adaptive tokens (open source)
2. Citrix virtual apps and desktops 2203 clipboard redirection policy