Skip to content

Commit 31303ca

Browse files
committed
[Silver III] Title: 피보나치 함수, Time: 100 ms, Memory: 14036 KB -BaekjoonHub
1 parent 0894089 commit 31303ca

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

백준/Silver/1003. 피보나치 함수/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
### 성능 요약
66

7-
메모리: 14092 KB, 시간: 124 ms
7+
메모리: 14036 KB, 시간: 100 ms
88

99
### 분류
1010

1111
다이나믹 프로그래밍
1212

13+
### 제출 일자
14+
15+
2025년 7월 26일 22:25:12
16+
1317
### 문제 설명
1418

1519
<p>다음 소스는 N번째 피보나치 수를 구하는 C++ 함수이다.</p>
Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
1-
import java.io.BufferedReader;
2-
import java.io.InputStreamReader;
3-
import java.util.Arrays;
1+
import java.io.*;
2+
import java.util.*;
43

54
public class Main {
6-
static int ONE[], Zero[];
7-
5+
static int t;
6+
static int[] num;
7+
static int[][] fibonArr;
8+
static StringBuilder sb = new StringBuilder();
9+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
811
public static void main(String[] args) throws Exception{
9-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10-
StringBuilder sb = new StringBuilder();
11-
int TC = Integer.parseInt(br.readLine());
12-
int n;
13-
14-
for (int t = 0; t < TC; t++) {
15-
n = Integer.parseInt(br.readLine());
16-
if(n != 0) {
17-
ONE = new int[n + 1];
18-
Zero = new int[n + 1];
19-
20-
ONE[0] = 0;
21-
ONE[1] = 1;
22-
Zero[0] = 1;
23-
Zero[1] = 0;
24-
25-
ONE[n] = fibonacciONE(n);
26-
Zero[n] = fibonacciZero(n);
27-
sb.append(Zero[n]).append(" ").append(ONE[n]).append("\n");
28-
29-
}else sb.append("1 0\n");
12+
inputSetting();
13+
14+
for(int i = 0; i < 41; i++) {
15+
fibonArr[0][i] = fibonacciZero(i);
16+
fibonArr[1][i] = fibonacciOne(i);
3017
}
31-
System.out.println(sb);
3218

19+
for(int i = 0; i < t; i++) sb.append(fibonArr[0][num[i]]).append(" ").append(fibonArr[1][num[i]]).append("\n");
20+
bw.append(sb);
21+
bw.close();
22+
}
23+
24+
static int fibonacciZero(int now){
25+
if(now < 0) return 0;
26+
if(fibonArr[0][now] != -1) return fibonArr[0][now];
27+
28+
if(now == 0) return 1;
29+
30+
return fibonArr[0][now] = fibonacciZero(now - 1) + fibonacciZero(now - 2);
3331
}
3432

35-
static int fibonacciONE(int n){
36-
if(n > 1 && ONE[n] == 0)
37-
ONE[n] = fibonacciONE(n - 1) + fibonacciONE(n - 2);
38-
return ONE[n];
33+
static int fibonacciOne(int now){
34+
if(now <= 0) return 0;
35+
if(fibonArr[1][now] != -1) return fibonArr[1][now];
36+
37+
if(now == 1) return 1;
38+
39+
return fibonArr[1][now] = fibonacciOne(now - 1) + fibonacciOne(now - 2);
3940
}
40-
static int fibonacciZero(int n){
41-
if(n > 1 && Zero[n] == 0)
42-
Zero[n] = fibonacciZero(n - 1) + fibonacciZero(n - 2);
43-
return Zero[n];
41+
42+
43+
static void inputSetting() throws Exception{
44+
t = Integer.parseInt(br.readLine());
45+
num = new int[t];
46+
fibonArr = new int[2][41];
47+
48+
for(int i = 0; i < 2; i++) Arrays.fill(fibonArr[i], -1);
49+
for(int i = 0; i < t; i++) num[i] = Integer.parseInt(br.readLine());
50+
51+
fibonArr[0][0] = 1;
52+
fibonArr[0][1] = 0;
53+
fibonArr[1][0] = 0;
54+
fibonArr[1][1] = 1;
4455
}
4556
}

0 commit comments

Comments
 (0)