Problem Solving/Java

백준 9095 1, 2, 3 더하기동적계획법 활용 2가지 풀이 ( Java )

TakeKnowledge 2019. 10. 25. 15:46
반응형
 

9095번: 1, 2, 3 더하기

문제 정수 4를 1, 2, 3의 합으로 나타내는 방법은 총 7가지가 있다. 합을 나타낼 때는 수를 1개 이상 사용해야 한다. 1+1+1+1 1+1+2 1+2+1 2+1+1 2+2 1+3 3+1 정수 n이 주어졌을 때, n을 1, 2, 3의 합으로 나타내는 방법의 수를 구하는 프로그램을 작성하시오. 입력 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 정수 n이 주어진다. n은 양수이며 11보다 작다. 출력 각

www.acmicpc.net

- Bottom-up ( 반복문 활용 ) 방식 

 

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
import java.io.*;
 
public class Main {
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
        int times = Integer.parseInt(br.readLine());
 
        int[] d = new int[11];
 
        d[0= 1;
        // n 으로 0이 들어올 경우는 아무것도 하지 않는 한가지 방법이 있어야 하나] 
        d[1= 1;
        d[2= 2;
        d[3= 4;
        // 1,2,3이 입력으로 들어올 때 기저사례 세팅
 
        for (int i = 4; i < 11; i++) {
            d[i] = d[i - 1+ d[i - 2+ d[i - 3];
            // 기저사례 활용해 4부터 최대 입력인 11까지 구해서 배열에 저장
        }
 
        for (int i = 0; i < times; i++) {
 
            int n = Integer.parseInt(br.readLine());
            // 테스트 케이스 받고
            bw.write(String.valueOf(d[n]));
            // 출력
            bw.write('\n');
            bw.flush();
 
        }
 
    }
 
}

 

- Top-Down ( 재귀 호출 활용 ) 방식 

 

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.*;
 
public class Main {
 
    public static int[] memo;
 
    public static int plus(int n) {
        
        if (memo[n] > 0) {
            // 메모에 값이 있으면
            return memo[n];
            // 메모값 리턴
        }
 
        if (n == 0) {
            memo[n] = 1;
            return memo[n];
        }
 
        if (n == 1) {
            memo[n] = 1;
            return memo[n];
        }
 
        if (n == 2) {
            memo[n] = 2;
            return memo[n];
        }
 
        if (n == 3) {
            memo[n] = 4;
            return memo[n];
        }
        // 기저 사례 세팅
 
        memo[n] = plus(n-1+ plus(n-2+ plus(n-3);
        // 끝에 1이 오는 경우 , 끝에 2가 오는 경우 , 끝에 3이 오는 경우일 때의 방법들을 모두 합하면
        // n일 때 값을 구할 수 있다 
 
        return memo[n];
    }
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
        int times = Integer.parseInt(br.readLine());
 
        memo = new int[11];
 
        for (int i = 0; i < times; i++) {
 
            int n = Integer.parseInt(br.readLine());
            // 테스트 케이스 받고
 
            int answer = plus(n);
 
            bw.write(String.valueOf(answer));
            // 출력
            bw.write('\n');
            bw.flush();
 
        }
 
    }
 
}

 

아직도 Bottom-up 방식이 편하지만 슬슬 재귀 호출이 직관적으로 다가오는 것 같다

반응형