[#16][알고리즘] 멀리 뛰기 (Dynamic Programming, 동적 프로그래밍)
프로그래머스 > 멀리 뛰기 (Dynamic Programming, 동적 프로그래밍)
문제 링크(https://programmers.co.kr/learn/challenge_codes/153)C++ 풀이
#include<iostream>
#include<vector>
using namespace std;
int jumpCase(int n){
int dp[n];
dp[0] = 1;
dp[1] = 2;
for(int i = 2; i < n; i++){
dp[i] = dp[i-2]+dp[i-1];
}
return dp[n-1];
}
int main()
{
int test = 4;
//아래는 테스트로 출력해 보기 위한 코드입니다.
cout << jumpCase(test);
}
| cs |
=> 피보나치 수열 동적 프로그래밍 풀이와 동일
더 간결한 풀이
#include<iostream>
#include<vector>
using namespace std;
int jumpCase(int n){
return n < 2 ? 1 : jumpCase(n-2) + jumpCase(n-1);
}
int main()
{
int test = 4;
//아래는 테스트로 출력해 보기 위한 코드입니다.
cout << jumpCase(test);
}
| cs |
댓글
댓글 쓰기