본문 바로가기
PS/백준

백준 - 알고리즘 수업 - 피보나치 수1

by 종안이 2023. 11. 12.

 

재귀함수와 Dp가 수행시간이 얼마나 차이가 나는지 분석한 문제이다. 

import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main {


    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));


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


        System.out.println(fib(num));
        System.out.println(num-2);
    }

    static int fib(int N) {
        if (N == 1 || N == 2) {
            return 1;
        } else {
            return (fib(N - 1) + fib(N - 2));
        }
    }



}

'PS > 백준' 카테고리의 다른 글

백준 - 수학은 체육과목 입니다  (0) 2023.11.12
백준 - 삼각형 외우기  (0) 2023.11.12
백준 - 직사각형 탈출  (1) 2023.11.11
백준 - 주사위 세개  (0) 2023.11.11
백준 - 최소공배수  (0) 2023.11.10

댓글