본문 바로가기
알고리즘 & 자료구조/백준

백준 9461

by 신재권 2022. 6. 27.
package baekjoon.DP;

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

public class Main9461 {

   static long[] D;

   public static void main(String[] args) throws Exception {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int T = Integer.parseInt(br.readLine());
      while (T-- > 0) {
         int N = Integer.parseInt(br.readLine());
         D = new long[N + 1];
         System.out.println(go(N));
      }
   }

   static long go(int N) {
      if (N == 1 || N == 2 || N == 3)
         return 1;
      if (N == 4 || N == 5)
         return 2;
      if (N == 6)
         return 3;
      if (D[N] > 0)
         return D[N];
      return D[N] = go(N - 5) + go(N - 1);
   }

   //D[1] = 1
   //D[2] = 1
   //D[3] = 1
   //D[4] = 2
   //D[5] = 2
   //D[6] = 3
   //D[7] = 4
   //D[8] = 5
   //D[9] = 7
   //D[10] = 9
   //D[11] = 12

   //D[1]+D[5] = 1+2 = 3 = D[6]
   //D[2]+D[6] = 1+3 = 4 = D[7]
   //D[3]+D[7] = 1+4 = 5 = D[8]
   //D[4]+D[8] = 2+5 = 7 = D[9]
   //D[5]+D[9] = 2+7 = 9 = D[10]

}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 1182  (0) 2022.06.28
백준 1912  (0) 2022.06.27
백준 1904  (0) 2022.06.27
백준 9184  (0) 2022.06.27
백준 2525  (0) 2022.06.26