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

알파·2022년 6월 2일
0

Algorithm

목록 보기
3/20

dp[n-3]에 1을 더한 경우, dp[n-2]에 2를 더한 경우, dp[n-1]에 3을 더한 경우, 세 가지의 합이 점화식이 되는 문제였다.

점화식: dp[n-3]+dp[n-2]+dp[n-1]

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        int[] dp = new int[12];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;
        for(int i = 4; i < 12; i++) {
            dp[i] = dp[i-3] + dp[i-2] + dp[i-1];
        }
        for(int i = 0; i < T; i++) {
            System.out.println(dp[Integer.parseInt(br.readLine())]);
        }
    }
}
profile
I am what I repeatedly do

0개의 댓글