개발자 첫걸음/백준

[BOJ 9095] 1, 2, 3 더하기

프로아마추어 2022. 2. 23. 16:42

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

 

9095번: 1, 2, 3 더하기

각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 출력한다.

www.acmicpc.net

 

<문제>

 

 

 

<나의 코드>

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(br.readLine());
		int[] d = new int[11];
		
		d[1] = 1;
		d[2] = 2;
		d[3] = 4;
		
		while(num > 0) {
			int n = Integer.parseInt(br.readLine());
			
			if(n>=4) {
				for(int i = 4; i <= n; i++) {
					if(d[i] > 0) continue;
					d[i] = d[i-1] + d[i-2] + d[i-3];
				}
			}
			
			System.out.println(d[n]);
			num--;
		}
	}
}

 

<제출결과>