BOJ_10870 피보나치 수 5 - Python3

해결 코드

1) 재귀 함수

import sys
input = sys.stdin.readline

def f(n):
    if n <=1:
        return n
    return f(n-1) + f(n-2)

n = int(input())
print(f(n))

2) for loop

import sys
input = sys.stdin.readline

n = int(input())
f = [0, 1]
for i in range(2, n+1):
    num = f[i-1] + f[i-2]
    f.append(num)
print(f[n])

'Etc > PS' 카테고리의 다른 글

BOJ_1260 DFS와 BFS - Python3  (0) 2022.07.22
BOJ_2441 별 찍기 - 4 - Python3  (0) 2022.07.20
BOJ_10026 적록색약 - Python3  (1) 2022.07.14
BOJ_2178 미로 탐색 - Python3  (0) 2022.07.14
BOJ_11279 최대 힙 - Python3  (1) 2022.07.11

+ Recent posts