Boj 9095 1,2,3 더하기

Updated:

문제 링크 : 백준 https://www.acmicpc.net/problem/9095

import java.io.*;

public class Main {

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


      memo = new int[12];
	  // 1,2,3으로 만들 수 있는 경우
      memo[1] = 1; //(1)
      memo[2] = 2; //(1,1),(2,0)
      memo[3] = 4; //(1,1,1),(2,1),(1,2),(3)

      int tc = Integer.parseInt(br.readLine());
      for(int t = 0; t<tc; t++) {
         int n = Integer.parseInt(br.readLine());
         System.out.println(find(n));
      }
   }

   private static int find(int n) {
      if(memo[n] > 0) return memo[n];

      memo[n] = find(n-1) + find(n-2) + find(n-3);
      return memo[n];
   }
}

Tags: , ,

Categories:

Updated:

Leave a comment