ALDS_10_A – フィボナッチ数
n = int(input())
fib = [1] * (n + 1)
for i in range(n + 1):
if i > 1:
fib[i] = fib[i-1] + fib[i-2]
print(fib[n])
import itertools
n = int(input())
m = list(itertools.repeat(-1, n + 1))
def fib(n):
if n == 0 or n == 1:
return 1
if m[n] != -1:
return m[n]
m[n] = fib(n-1) + fib(n-2)
return m[n]
print(fib(n))
コメント