当前位置:网站首页>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);
}
};
边栏推荐
- Download address of each version of libtorch
- In the "meta cosmic space", utonmos will open the digital world of the combination of virtual and real
- Ncnn compilation and use pnnx compilation and use
- The finished product of wechat campus laundry applet graduation design (1) development outline
- 达科为生物IPO过会:年营收8.37亿 吴庆军父女为实控人
- 【实习经验】Date工具类中添加自己实现的方法
- 不需要标注数据的语义分割!ETH&鲁汶大学提出MaskDistill,用Transformer来进行无监督语义分割,SOTA!...
- 592. 分数加减运算
- 期货手续费标准和保证金比例
- Charles tutorial
猜你喜欢

Wechat campus laundry applet graduation design finished product (6) opening defense ppt

阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%

Good architecture is evolved, not designed

SQL tutorial: introduction to SQL aggregate functions

特征工程中的缩放和编码的方法总结

【idea】设置提取serialVersionUID

SNMP (Simple Network Management Protocol)

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

The salary level of programmers in various countries is a little miserable

Interview secrets are widely distributed, and the exclusive secrets of editing, testing and learning are leaked?!
随机推荐
RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
小程序毕设作品之微信校园洗衣小程序毕业设计成品(1)开发概要
new的多种使用方法
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
GoPro接入 - 根据GoPro官方文档/Demo,实现对GoPro的控制和预览
基于C语言的LR1编译器设计
We should learn to check the documented instructions of technical details
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%
小程序毕设作品之微信校园洗衣小程序毕业设计成品(5)任务书
Wechat campus laundry applet graduation design finished product (5) assignment
Creation and destruction of "C language" function stack frame -- (internal skill)
《C语言》函数栈帧的创建与销毁--(内功)
Various ways to use new
16 VMware horizon 2203 virtual desktop-win10 automatic desktop pool full clone dedicated (XVI)
Brief tutorial for soft exam system architecture designer | system design
JWT login expiration - automatic refresh token scheme introduction
redis集群搭建-使用docker快速搭建一个测试redis集群
The finished product of wechat campus laundry applet graduation design (1) development outline
What are the benefits of taking NPDP