-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path592.cpp
34 lines (34 loc) · 786 Bytes
/
592.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// gcd+lcm.cpp
class Solution {
public:
string fractionAddition(string expression) {
stringstream ss(expression);
int a, b, suma = 0, sumb = 1;
char ch;
while (ss >> a >> ch >> b) {
int _lcm = lcm(b, sumb);
suma *= _lcm / sumb;
sumb = _lcm;
a *= _lcm / b;
suma += a;
int _gcd = gcd(suma, sumb);
suma /= _gcd;
sumb /= _gcd;
}
if (suma * sumb < 0)
return "-" + to_string(abs(suma)) + "/" + to_string(abs(sumb));
return to_string(abs(suma)) + "/" + to_string(abs(sumb));
}
int gcd(int a, int b) {
if (a < b)
return gcd(b, a);
int temp;
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
};