본문 바로가기
아기 개발자/백준 문제풀이

[BOJ_JAVA] 백준 10870번 : 피보나치 수 5 @달깅

by 달깅 2019. 11. 4.

https://www.acmicpc.net/problem/10870

 

피보나치 수도 항상 수업시간에 배우는 재귀함수..

그래도 다시 짜보려고 하니까 헷갈렸다!

내가 피보나치 수를 잘 몰라서 그런것..ㅠㅠ

 


정답코드

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println(fibo(n));
    }

    public static int fibo(int n) {
        if (n==0) {
            return 0;
        } else if (n==1 || n==2) {
            return 1;
        } else {
            return fibo(n-1)+fibo(n-2);
        }
    }
}

 

피보나치 수 정복ㅎ

댓글