forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5.java
More file actions
30 lines (24 loc) Β· 991 Bytes
/
5.java
File metadata and controls
30 lines (24 loc) Β· 991 Bytes
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
import java.util.*;
public class Main {
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
public static int[] d = new int[30001];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
for (int i = 2; i <= x; i++) {
// νμ¬μ μμμ 1μ λΉΌλ κ²½μ°
d[i] = d[i - 1] + 1;
// νμ¬μ μκ° 2λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 2 == 0)
d[i] = Math.min(d[i], d[i / 2] + 1);
// νμ¬μ μκ° 3μΌλ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 3 == 0)
d[i] = Math.min(d[i], d[i / 3] + 1);
// νμ¬μ μκ° 5λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 5 == 0)
d[i] = Math.min(d[i], d[i / 5] + 1);
}
System.out.println(d[x]);
}
}