Skip to content

Commit 398e922

Browse files
committed
issue #42 피보나치 함수
1 parent 4728aa6 commit 398e922

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

‎src/backjoon/_1003.java‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/1003
3+
// 피보나치 함수
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
8+
public class _1003 {
9+
public static int cntZero = 0;
10+
public static int cntOne = 0;
11+
static int zero_plus_one;
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
int T = Integer.parseInt(br.readLine());
17+
StringBuilder sb = new StringBuilder();
18+
19+
for(int i=0; i<T; i++){
20+
int n = Integer.parseInt(br.readLine());
21+
fibonacci(n);
22+
sb.append(cntZero).append(' ').append(cntOne).append("\n");
23+
// cntZero = 0;
24+
// cntOne = 0;
25+
}
26+
System.out.println(sb);
27+
}
28+
29+
// sol1 시간초과
30+
/*
31+
static int fibonacci(int n){
32+
if(n == 0){
33+
cntZero++;
34+
return 0;
35+
} else if (n == 1){
36+
cntOne++;
37+
return 1;
38+
} else {
39+
return fibonacci(n-1) + fibonacci(n-2);
40+
}
41+
}
42+
*/
43+
44+
// so2 memory 11432 runtime 76
45+
// 규칙을 찾아서 풀기
46+
static void fibonacci(int N) {
47+
// 반드시 초기화 해야한다.
48+
cntZero = 1;
49+
cntOne = 0;
50+
zero_plus_one = 1;
51+
52+
for (int i = 0; i < N; i++) {
53+
cntZero = cntOne;
54+
cntOne = zero_plus_one;
55+
zero_plus_one = cntZero + cntOne;
56+
}
57+
58+
}
59+
}
60+
/*
61+
input
62+
3
63+
0
64+
1
65+
3
66+
67+
output
68+
1 0
69+
0 1
70+
1 2
71+
*/

0 commit comments

Comments
 (0)