forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.py
More file actions
16 lines (14 loc) ยท 593 Bytes
/
2.py
File metadata and controls
16 lines (14 loc) ยท 593 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# ํ ๋ฒ ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ชจ์ด์ ์ด์
(Memoization)ํ๊ธฐ ์ํ ๋ฆฌ์คํธ ์ด๊ธฐํ
d = [0] * 100
# ํผ๋ณด๋์น ํจ์(Fibonacci Function)๋ฅผ ์ฌ๊ทํจ์๋ก ๊ตฌํ (ํ๋ค์ด ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ)
def fibo(x):
# ์ข
๋ฃ ์กฐ๊ฑด(1 ํน์ 2์ผ ๋ 1์ ๋ฐํ)
if x == 1 or x == 2:
return 1
# ์ด๋ฏธ ๊ณ์ฐํ ์ ์๋ ๋ฌธ์ ๋ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
if d[x] != 0:
return d[x]
# ์์ง ๊ณ์ฐํ์ง ์์ ๋ฌธ์ ๋ผ๋ฉด ์ ํ์์ ๋ฐ๋ผ์ ํผ๋ณด๋์น ๊ฒฐ๊ณผ ๋ฐํ
d[x] = fibo(x - 1) + fibo(x - 2)
return d[x]
print(fibo(99))