끄적끄적 코딩
article thumbnail
728x90

 

다이나믹 프로그래밍 문제입니다.

 

파도반 수열 n길이에서 n번째 수를 찾는 문제입니다.

파도반 수열 첫 10개의 숫자는

1, 1, 1, 2, 2, 3, 4, 5, 7, 9입니다.

여기서 6번째 자리숫자부터는

현재위치 전의 값 + 현재위치 -5번째의 값을 더한 값이 나옵니다.

 

이를 코드로 하면

DP[i] = DP[i-1] + DP[i-5]

 

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
   int n;
   int len;
   long long int DP[110] = { 0, };
   cin >> n;

   DP[1] = 1;
   DP[2] = 1;
   DP[3] = 1;
   DP[4] = 2;
   DP[5] = 2;

   while (n-- > 0) {
      cin >> len;
      
      if (DP[len] != 0) {
         cout << DP[len] << endl;
         continue;
      }
      for (int i = 6; i <= len; ++i) {
         DP[i] = DP[i - 1] + DP[i - 5];
      }
      cout << DP[len] << endl;
   }
   return 0;
}

'알고리즘' 카테고리의 다른 글

[C++] 백준 2294번 동전 2  (0) 2019.05.27
[C++] 백준 2167번 2차원 배열의 합  (0) 2019.05.27
[C++] 백준 14501번 퇴사  (0) 2019.05.25
[C++] 백준 1010번 다리 놓기  (0) 2019.05.24
[C++] 백준 11057번 오르막 수  (0) 2019.05.24

검색 태그