forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5.py
More file actions
21 lines (18 loc) Β· 669 Bytes
/
5.py
File metadata and controls
21 lines (18 loc) Β· 669 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# μ μ Xλ₯Ό μ
λ ₯ λ°κΈ°
x = int(input())
# μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
d = [0] * 1000001
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
for i in range(2, x + 1):
# νμ¬μ μμμ 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)
print(d[x])