Dev.baelanche

[백준 10870] 피보나치 수 5 본문

Data Structure & Algorithm/PS - JAVA

[백준 10870] 피보나치 수 5

baelanche 2019. 6. 25. 23:06
반응형

 

n이 최대 20이라 문제에 나와있는 점화식을 가지고 재귀하여 풀면 된다.

 

나는 굳이 탑다운 방식으로 풀었다.

 

public class Main {
    
    static int dp[];
    
    public static int f(int k) {
        if(k == 0) return 0;
        if(k == 1) return 1;
        if(dp[k] != -1) return dp[k];
        
        int ret = f(k-1) + f(k-2);
        dp[k] = ret;
        return ret;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        dp = new int[n+1];
        Arrays.fill(dp, -1);
        
        System.out.println(f(n));
    }
}
반응형

'Data Structure & Algorithm > PS - JAVA' 카테고리의 다른 글

[백준 15685] 드래곤 커브  (0) 2019.06.29
[백준 11729] 하노이 탑 이동 순서  (0) 2019.06.25
[백준 2562] 최댓값  (0) 2019.06.25
[백준 2753] 윤년  (0) 2019.06.25
[백준 2588] 곱셈  (0) 2019.06.25
Comments