JAVA | 백준 10870 | 피보나치 수
1. Scanner 이용
import java.util.Scanner;
public class Main {
static int fibonacci(int N){
if(N==0) return 0;
if(N==1) return 1;
return fibonacci(N-1)+fibonacci(N-2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
System.out.println(fibonacci(N));
}
}
2. Buffer 이용
import java.io.*;
public class Main {
static int fibonacci(int N){
if(N == 0) return 0;
if(N == 1) return 1;
return fibonacci(N-1) + fibonacci(N-2);
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
System.out.println(fibonacci(N));
}
}