컴퓨터공학 💻 도서관📚
Part2. 8-7 피보나치 수열 문제 여러 방식으로 해결하기 본문

// 재귀함수 버전
public int fibonacciRecur(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacciRecur(n - 1) + fibonacciRecur(n - 2);
}
// 반복문 버전
public int fibonacciIter(int n) {
int ppre = 0;
int pre = 1;
int current = 0;
if (n == 0) return 0;
if (n == 1) return 1;
for (int i = 2; i <= n; i++) {
current = ppre + pre;
ppre = pre;
pre = current;
}
return current;
}
// 반복문 + 배열 버전
public int fibonacciMem(int n) {
value[0] = 0;
value[1] = 1;
if (n == 0) {
return value[0];
}
if (n == 1) {
return value[1];
}
int i;
for( i = 2; i<=n; i++) {
value[i] = value[i-1] + value[i-2];
}
return value[i-1];
}'✅🌲강의 복습 노트 > 패캠 JavaSpring 강의,코드 복습' 카테고리의 다른 글
| Part2. 8-9 경우의 수 문제 (Brute-Force Search : 완전탐색) (0) | 2026.01.11 |
|---|---|
| Part2. 8-8 그리디 알고리즘(여러 종류의 동전으로 가격 지불하는 문제) (0) | 2026.01.11 |
| Part2. 8-6 미로 찾기 (0) | 2026.01.08 |
| Part2. 8-5 최단거리 (다익스트라 알고리즘) (0) | 2026.01.05 |
| Part2. 8-3 정렬 알고리즘 문제 (0) | 2026.01.02 |
Comments
