피보나치 수 - [DP] - Java :: 매운코딩
728x90
300x250

shoark7.github.io/programming/algorithm/%ED%94%BC%EB%B3%B4%EB%82%98%EC%B9%98-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98%EC%9D%84-%ED%95%B4%EA%B2%B0%ED%95%98%EB%8A%94-5%EA%B0%80%EC%A7%80-%EB%B0%A9%EB%B2%95

 

피보나치 수열 알고리즘을 해결하는 5가지 방법

Let me introduce 5 different ways to solve fibonacci algorithm

shoark7.github.io

public class Fun4150 {

	public static int n;
	public static int[] dp;
	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream("input.txt"));
		Scanner sc = new Scanner(System.in);
		
		n = sc.nextInt();
		dp = new int[n+1];
		long ans = solve(n);

		System.out.println(ans);
	}
	
	public static int solve(int num) {
		if(dp[num]!=0)
			return dp[(int) num];
		
		if(num<=1) {
			dp[num] = num;
			return num;
		}
		else {
			dp[num] =solve(num-1) + solve(num-2);
			return dp[num];
			
		}
			
	}

}
728x90

+ Recent posts