백준/브루트포스

[백준] 9095 - 1, 2, 3 더하기 [JAVA]

odong2 2024. 5. 8. 16:45

 

 

코드

import java.io.*;

public class Main{

    static Integer[] dp;

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        dp = new Integer[11];

        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;

        for (int i = 0; i < N; i++) {
            int T = Integer.parseInt(br.readLine());

            System.out.println(solution(T));
        }
    }

    static int solution(int n) {

        if (n == 0) {
            return 0;
        }

        if (dp[n] == null) {
            dp[n] = solution(n - 1) + solution(n - 2) + solution(n - 3);
        }

        return dp[n];
    }
}