tongnamuu 2018. 11. 12. 15:28

https://www.acmicpc.net/problem/1463

주어진 조건에 따라 점화식을 세우면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int d[n + 1= { 0, };
    for (int i = 0; i < n; i++)
        d[i] = n + 1;
    d[n] = 0;
    for (int i = n; i >= 1; i--) {
        if (i % 3 == 0) {
            if (d[i / 3> d[i] + 1)
                d[i / 3= d[i] + 1;
        }
        if (i % 2 == 0) {
            if (d[i / 2> d[i] + 1)
                d[i / 2= d[i] + 1;
        }
        if (d[i - 1> d[i] + 1)
            d[i - 1= d[i] + 1;
    }
    cout << d[1<< '\n';
}
cs