Created
June 1, 2017 14:23
-
-
Save developer-sdk/e883a3a063d54c02f7d46878003c769a to your computer and use it in GitHub Desktop.
백준, 1003, 다이나믹프로그래밍, 피보나치 함수
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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