Updated:

1. 문제 링크

https://www.acmicpc.net/problem/9095

2. 사용 알고리즘

DP

3. 풀이

d[n] : n을 1, 2, 3의 합으로 나타내는 방법의 수

  • n - 3에 3을 더해서 n을 만드는 방법 : d[n - 3]

  • n - 2에 2를 더해서 n을 만드는 방법 : d[n - 2]

  • n - 1에 1을 더해서 n을 만드는 방법 : d[n - 1]

∴ d[n] = d[n - 3] + d[n - 2] + d[n - 1];

4. 소스 코드

4-1. C++

4-1-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/9095.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int d[13];

int solve(int num) {
    if (num == 1) return 1;
    else if (num == 2) return 2;
    else if (num == 3) return 4;
    if (d[num] > 0) return d[num];
    return d[num] = solve(num - 3) + solve(num - 2) + solve(num - 1);
}

int main(void) {
    ios_base::sync_with_stdio(false);
    int t; for (cin >> t; t--;) {
        int n; cin >> n;
        cout << solve(n) << "\n";
    }
    return 0;
}

4-1-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/9095_2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int d[13];

int main(void) {
    ios_base::sync_with_stdio(false);
    d[1] = 1; d[2] = 2; d[3] = 4;
    for (int i = 4; i < 11; ++i) d[i] = d[i - 3] + d[i - 2] + d[i - 1];
    int t; for (cin >> t; t--;) {
        int n; cin >> n;
        cout << d[n] << "\n";
    }
    return 0;
}

4-2. JAVA

4-2-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/9095.java

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static int[] d = new int[13];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int t; for(t = Integer.parseInt(br.readLine()); t-- > 0;) {
            int n = Integer.parseInt(br.readLine());
            sb.append(solve(n)).append("\n");
        }
        System.out.println(sb);
    }

    public static int solve(int num) {
        if(num == 1) return 1;
        else if(num == 2) return 2;
        else if(num == 3) return 4;
        if(d[num] > 0) return d[num];
        return d[num] = solve(num - 3) + solve(num - 2) + solve(num - 1);
    }
}

4-2-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/9095_2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static int[] d = new int[13];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        d[1] = 1; d[2] = 2; d[3] = 4;
        for(int i = 4; i < 11; ++i) d[i] = d[i - 3] + d[i - 2] + d[i - 1];
        int t; for(t = Integer.parseInt(br.readLine()); t-- > 0;) {
            int n = Integer.parseInt(br.readLine());
            sb.append(d[n]).append("\n");
        }
        System.out.println(sb);
    }
}

Updated:

Leave a comment