Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created June 1, 2017 14:23
Show Gist options
  • Select an option

  • Save developer-sdk/e883a3a063d54c02f7d46878003c769a to your computer and use it in GitHub Desktop.

Select an option

Save developer-sdk/e883a3a063d54c02f7d46878003c769a to your computer and use it in GitHub Desktop.
백준, 1003, 다이나믹프로그래밍, 피보나치 함수
import java.util.Scanner;
/**
* 백준, 1003, 피보나치 함수
*
* @author whitebeard
*
*/
public class Problem1003 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int[][] dp = new int[41][2];
dp[0][0] = 1;
dp[1][1] = 1;
for (int i = 0; i < T; i++) {
fibonacciPrint(dp, sc.nextInt());
}
sc.close();
}
public static void fibonacciPrint(int[][] dp, int n) {
if (n >= 2 && dp[n][0] == 0 && dp[n][1] == 0) {
for (int i = 2; i <= n; i++) {
dp[i][0] = dp[i - 1][0] + dp[i - 2][0];
dp[i][1] = dp[i - 1][1] + dp[i - 2][1];
}
}
System.out.printf("%d %d\n", dp[n][0], dp[n][1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment