본문 바로가기
PS/백준

백준 - 피보나치 수 2 (Java)

by 종안이 2023. 11. 26.

 

피보나치 수를 구하는 문제인데 DP를 사용했다 . 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {


    static Long dp[];


    public static void main(String[] args) throws IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));


        int num = Integer.parseInt(bf.readLine());

        dp = new Long[num + 1];

        dp[0] = 0L;
        dp[1] = 1L;

        if (num >= 2) {
            System.out.println(find(num));
        } else if(num == 0){
            System.out.println(0);
        } else if(num == 1){
            System.out.println(1);
        }
    }

    static long find(int N) {

        if (dp[N] == null) {
            dp[N] = find(N - 1) + find(N - 2);
        }
        return dp[N];
    }
}

댓글