Skip to content

Commit f06b1dc

Browse files
authored
Update 3.py
1 parent 5390fc2 commit f06b1dc

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

8/3.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
# 한 번 계산된 결과를 메모이제이션(Memoization)하기 위한 리스트 초기화
1+
# 한 번 계산된 결과를 저장하기 위한 DP 테이블 초기화
22
d = [0] * 100
33

4-
# 첫 번째 피보나치 수와 두 번째 피보나치 수는 1
5-
d[1] = 1
6-
d[2] = 1
7-
n = 99
4+
# 피보나치 함수(Fibonacci Function)를 재귀함수로 구현 (다이나믹 프로그래밍)
5+
def fibo(x):
6+
# 종료 조건(1 혹은 2일 때 1을 반환)
7+
if x == 1 or x == 2:
8+
return 1
9+
# 이미 계산한 적 있는 문제라면 그대로 반환
10+
if d[x] != 0:
11+
return d[x]
12+
# 아직 계산하지 않은 문제라면 점화식에 따라서 피보나치 결과 반환
13+
d[x] = fibo(x - 1) + fibo(x - 2)
14+
return d[x]
815

9-
# 피보나치 함수(Fibonacci Function) 반복문으로 구현
10-
for i in range(3, n + 1):
11-
d[i] = d[i - 1] + d[i - 2]
12-
13-
print(d[n])
16+
print(fibo(99))

0 commit comments

Comments
 (0)