simplified-fractions 1.0.0
Simplified Fractions
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
6{
7public:
8 int gcd(int a, int b)
9 {
10 while (b != 0)
11 {
12 int tmp = b;
13 b = a % b;
14 a = tmp;
15 }
16 return a;
17 }
18
19 vector<string> simplifiedFractions(int n)
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 }
47};
48
49int main()
50{
51 vector<string> answer = Solution().simplifiedFractions(4);
52 for (string& s: answer)
53 cout << s << " ";
54 cout << '\n';
55 return 0;
56}
vector< string > simplifiedFractions(int n)
Definition main.cpp:19
int gcd(int a, int b)
Definition main.cpp:8
int main()
Definition main.cpp:49