-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq17.9.cpp
55 lines (39 loc) · 1.07 KB
/
q17.9.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(void) {
int k;
cout << "enter the value of k: ";
cin >> k;
queue<int> queue3;
queue<int> queue5;
queue<int> queue7;
vector<int> multiples{1};
queue3.push(3);
queue5.push(5);
queue7.push(7);
queue<int> *min_queue;
while ((int)multiples.size() <= k) {
if (queue3.front() < queue5.front())
min_queue = &queue3;
else
min_queue = &queue5;
if (min_queue->front() > queue7.front())
min_queue = &queue7;
multiples.push_back(min_queue->front());
if (min_queue->front() == queue3.front())
queue3.push(3 * min_queue->front());
if (min_queue->front() != queue7.front())
queue5.push(5 * min_queue->front());
queue7.push(7 * min_queue->front());
min_queue->pop();
}
for (auto &elem : multiples) {
cout << elem << ", ";
}
cout << endl;
cout << "kth number with only 3, 5 and/or 7 as prime factors is: "
<< multiples.back() << endl;
return 0;
}