simplified-fractions 1.0.0
Simplified Fractions
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

int gcd (int a, int b)
 
vector< string > simplifiedFractions (int n)
 

Detailed Description

Definition at line 5 of file main.cpp.

Member Function Documentation

◆ gcd()

int Solution::gcd ( int  a,
int  b 
)
inline

Definition at line 8 of file main.cpp.

9 {
10 while (b != 0)
11 {
12 int tmp = b;
13 b = a % b;
14 a = tmp;
15 }
16 return a;
17 }

Referenced by simplifiedFractions().

◆ simplifiedFractions()

vector< string > Solution::simplifiedFractions ( int  n)
inline

Definition at line 19 of file main.cpp.

20 {
21 queue<pair<int, int>> to_process;
22 for (int i = 1; i <= n - 1; ++i)
23 {
24 for (int j = 2; j <= n; ++j)
25 {
26 if (j <= i)
27 continue;
28 to_process.push({i, j});
29 }
30 }
31
32 vector<string> answer;
33 answer.reserve(to_process.size());
34
35 while (!to_process.empty())
36 {
37 if (gcd(to_process.front().first, to_process.front().second) == 1)
38 {
39 string str = to_string(to_process.front().first) + '/' + to_string(to_process.front().second);
40 answer.push_back(str);
41 }
42 to_process.pop();
43 }
44
45 return answer;
46 }
int gcd(int a, int b)
Definition main.cpp:8

References gcd().

Referenced by main().


The documentation for this class was generated from the following file: