[Codewars] Common Denominators

Common Denominators

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;
class Fracts
{
static string frac(unsigned long long n, unsigned long long d) {
return "(" + to_string(n) + "," + to_string(d) + ")";
}
public:
static std::string convertFrac(std::vector<std::vector<unsigned long long>> &lst) {
unsigned long long lcm = lst[0][1];
for(auto fraction : lst) {
unsigned long long g = __gcd(fraction[1], lcm);
lcm = lcm / g * fraction[1];
}
string res = "";
for(auto& fraction : lst) {
unsigned long long x = lcm / fraction[1];
res += frac(fraction[0] * x, lcm);
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/29/PS/Codewars/common-denominators/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.