1003번 피보나치 함수
2020. 2. 3. 14:49ㆍLearn/Algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class Main {
static int ar1[] = new int [41];
static int ar2[] = new int [41];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
ar1[0] = 1;
ar1[1] = 0;
ar2[0] = 0;
ar2[1] = 1;
for(int i = 2; i < 41; i++) {
ar1[i] = ar1[i-1] + ar1[i-2];
ar2[i] = ar2[i-1] + ar2[i-2];
}
for(int i = 0; i < a; i++) {
int b= sc.nextInt();
System.out.println(ar1[b] + " " + ar2[b]);
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
그냥 피보나치 함수를 재귀로 구현하면 터지는 문제.
배열을 통해서 1출력배열과 0출력배열을 만들어서 40까지 모든 필요한 피보나치 배열을 만들어준뒤
해당하는 값을 그냥 뽑아주는 알고리즘을 구현했다
'Learn > Algorithm' 카테고리의 다른 글
1260 DFS 와 BFS - 백준 (0) | 2020.02.04 |
---|---|
14720 우유축제 (0) | 2020.02.03 |
2869번 달팽이는 올라가고 싶다. (0) | 2020.02.03 |
2447 별찍기 -10 (0) | 2020.02.02 |
11053 백준 가장 긴 증가하는 부분수열 (0) | 2020.02.02 |