-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[12852] 1로 만들기 2
50 lines (42 loc) · 904 Bytes
/
[12852] 1로 만들기 2
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
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] dp = new int[n + 1];
int[] arr = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
dp[1] = 0;
for(int i=2; i<=n; i++) {
if(i%3 == 0) {
if(dp[i/3] + 1 < dp[i]) {
dp[i] = dp[i/3] + 1;
arr[i] = i/3;
}
}
if(i%2 == 0) {
if(dp[i/2] + 1 < dp[i]) {
dp[i] = dp[i/2] + 1;
arr[i] = i/2;
}
}
if(dp[i - 1] + 1 <dp[i]) {
dp[i] = dp[i - 1] + 1;
arr[i] = i - 1;
}
}
System.out.println(dp[n]);
printPath(arr, n);
}
public static void printPath(int[] arr, int n) {
if(n==1) {
System.out.println(n);
return;
}
System.out.print(n + " ");
printPath(arr, arr[n]);
}
}