-
Notifications
You must be signed in to change notification settings - Fork 0
/
1463_1로 만들기.cpp
64 lines (59 loc) · 1.05 KB
/
1463_1로 만들기.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
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
int findMin(int a, int b, int c)
{
int min = a;
if (b < min)
min = b;
if (c < min)
min = c;
return min;
}
int main(void)
{
int *dp = new int[1000000];
dp[1] = 1;
dp[2] = 1;
for (int i = 3; i < 1000000; i++)
{
int num = i + 1;
int count1 = 0; // -1
int count2 = 0; // /2
int count3 = 0; // /3
if (num % 3 == 0 || num % 2 == 0)
{
if (num % 3 == 0 && num % 2 == 0) //3, 2둘 다 나누어질 때
{
count1 = dp[num - 2];
count2 = dp[num / 2 - 1];
count3 = dp[num / 3 - 1];
}
else
{
if (num % 3 == 0) //3으로만 나누어질 때
{
count1 = dp[num - 2];
count2 = 1000000;
count3 = dp[num / 3 - 1];
}
else //2로만 나누어질 때
{
count1 = dp[num - 2];
count2 = dp[num / 2 - 1];
count3 = 1000000;
}
}
}
else //3, 2 둘 다 나누어지지 않을 때
{
count1 = dp[num - 2];
count2 = 1000000;
count3 = 1000000;
}
dp[i] = findMin(count1, count2, count3) + 1;
}
int input;
cin >> input;
cout << dp[input - 1];
return 0;
}