-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_12852.cpp
More file actions
59 lines (54 loc) · 1.07 KB
/
BOJ_12852.cpp
File metadata and controls
59 lines (54 loc) · 1.07 KB
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
56
57
58
59
// 22 / 1 / 3
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> check;
queue<int> q;
int main(void)
{
int n;
cin >> n;
check.assign(n + 1, 0);
q.push(n);
while (q.front() != 1)
{
int x = q.front();
q.pop();
if ((x % 3 == 0) && (check[x / 3] == 0))
{
check[x / 3] = 1;
q.push(x / 3);
}
if ((x % 2 == 0) && (check[x / 2] == 0))
{
check[x / 2] = 2;
q.push(x / 2);
}
if ((x > 1) && (check[x - 1] == 0))
{
check[x - 1] = 3;
q.push(x - 1);
}
}
vector<int> answer;
int x = 1;
int num = 0;
while (1)
{
answer.push_back(x);
if (x == n)
break;
if (check[x] == 1)
x *= 3;
else if (check[x] == 2)
x *= 2;
else
x += 1;
num++;
}
cout << num << "\n";
for (int i = (int)answer.size() - 1; i >= 0; i--)
cout << answer[i] << " ";
return 0;
}