Programming/Java

백준 10870 피보나치 수5 [java]

fishersheep 2022. 4. 4. 16:05
반응형
package hello;
import java.util.*;

public class Main {
	
	public static int func(int a)
	{
		if(a==0)
			return 0;
		else if(a==1)
			return 1;
		else
			return func(a-1) + func(a-2);
	}
	
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		System.out.print(func(n));
		
	}
	

}

 

후기

재귀함수를 활용하여 간단하게 구현 가능하다.

반응형