Created
December 24, 2021 13:04
-
-
Save key-del-jeeinho/47f5b2fdccae3f6b8f8362487c16b1ca to your computer and use it in GitHub Desktop.
[알고리즘 - 백준] 1003 피보나치 함수 Wrong Answer 질문
This file contains 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
//package baekjoon._1000_9999번대._1000번대._1003_피보나치_함수.지인호; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
public class Main { | |
public static int[] dp = new int[40]; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
Arrays.fill(dp, -1); | |
int t = sc.nextInt(); | |
for (int i = 0; i < t; i++) { | |
int n = sc.nextInt(); | |
f(n); | |
switch (n) { | |
case 0: | |
System.out.println("1 0"); | |
break; | |
case 1: | |
System.out.println("0 1"); | |
break; | |
default: | |
System.out.printf("%d %d\n", f(n - 1), f(n)); | |
} | |
} | |
} | |
private static int f(int n) { | |
if(n <= 1) return n; | |
if(dp[n-1] == -1) dp[n-1] = f(n-1) + f(n-2); | |
return dp[n-1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment