forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.cpp
26 lines (23 loc) Β· 793 Bytes
/
5.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
#include <bits/stdc++.h>
using namespace std;
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
int d[30001];
int x;
int main(void) {
cin >> x;
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
for (int i = 2; i <= x; i++) {
// νμ¬μ μμμ 1μ λΉΌλ κ²½μ°
d[i] = d[i - 1] + 1;
// νμ¬μ μκ° 2λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 2 == 0)
d[i] = min(d[i], d[i / 2] + 1);
// νμ¬μ μκ° 3μΌλ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 3 == 0)
d[i] = min(d[i], d[i / 3] + 1);
// νμ¬μ μκ° 5λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 5 == 0)
d[i] = min(d[i], d[i / 5] + 1);
}
cout << d[x] << '\n';
}