首页 技术 正文
技术 2022年11月18日
0 收藏 563 点赞 2,898 浏览 2056 个字

Given a string representing an expression of fraction addition and subtraction,
you need to return the calculation result in string format. The final result should be irreducible fraction.
If your final result is an integer, say 2, you need to change it to the format of fraction that has denominator 1.
So in this case, 2 should be converted to 2/1.
Example 1:
Input:”-1/2+1/2″
Output: “0/1”
Example 2:
Input:”-1/2+1/2+1/3″
Output: “1/3”
Example 3:
Input:”1/3-1/2″
Output: “-1/6”
Example 4:
Input:”5/3+1/3″
Output: “2/1”
Note:
The input string only contains ‘0’ to ‘9’, ‘/’, ‘+’ and ‘-‘. So does the output.
Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then ‘+’ will be omitted.
The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
The number of given fractions will be in the range [1,10].
The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

思路:

如下是参考一 大神给出的代码,先贴这儿,慢慢学习:

string fractionAddition(string s)
{
long p = , q = , p1, q1, t;
for (size_t i = , j; i < s.size(); i = j) {
j = s.find_first_of("+-", i+);
if (j == string::npos) j = s.size();
auto k = s.find('/', i);
long x = stol(s.substr(i, k-i)), y = stol(s.substr(k+, j));
p1 = p*y+q*x;
q1 = q*y;
t = __gcd(p1, q1);
p = p1/t;
q = q1/t;
if (q < ) p *= -, q *= -;
}
return to_string(p)+"/"+to_string(q);
}

如下是leetcode上的solution。

The initial fraction is 0/1 (n/d). We just need to read next fraction (nn/dd), normalize denominators between n/d and nn/dd (using GCD), and add/subtract the numerator (n +/- nn). In the end, we also need to use GCD to make the resulting fraction irreducible.

int GCD(int a, int b ){ return (b == ) ? a : GCD(b, a % b); }
string fractionAddition(string s) {
int n = , d = , p = , p1 = , p2 = ;
if (s[] != '-') s = "+" + s;
while (p < s.size()) {
for (p1 = p + ; s[p1] != '/'; ++p1);
for (p2 = p1 + ; p2 < s.size() && s[p2] != '+' && s[p2] != '-'; ++p2);
auto nn = stoi(s.substr(p + , p1 - p - )), dd = stoi(s.substr(p1 + , p2 - p1 - ));
auto gcd = GCD(d, dd);
n = n * dd / gcd + (s[p] == '-' ? - : ) * nn * d / gcd;
d *= dd / gcd;
p = p2;
}
auto gcd = GCD(abs(n), d);
return to_string(n / gcd) + "/" + to_string(d / gcd);
}

参考:

https://leetcode.com/maskray/

https://discuss.leetcode.com/topic/90024/c-12-lines-gcd

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918