-
Notifications
You must be signed in to change notification settings - Fork 1
/
2748.py
71 lines (48 loc) · 942 Bytes
/
2748.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
두 가지 방법으로 풀었음
"""
def fibo(n):
cnt = 0
f1 = 0
f2 = 1
while cnt < n:
cnt += 1
f1, f2 = f2, f1+f2
return f1
n = int(input())
print(fibo(n))
"""
메모제이션 방법
2020.02.10
"""
def main():
n = int(input())
memo = [-1 for i in range(n+1)]
memo[0], memo[1] = [0, 1]
#memoization
def fibo(n):
if n <= 1:
return memo[n]
elif memo[n] != -1:
return memo[n]
else:
memo[n] = fibo(n - 1) + fibo(n - 2)
return memo[n]
fibo(n)
print(memo[n])
"""
더 간결하게 작성
2021.02.03
"""
def main_2():
n = int(input())
dp = [None for _ in range(n+1)]
dp[0], dp[1] = [0, 1]
def calc_dp(n):
if dp[n] is None:
dp[n] = calc_dp(n-1) + calc_dp(n-2)
return dp[n]
calc_dp(n)
print(dp[n])
if __name__ == "__main__":
main()