Skip to content

Commit 6d89421

Browse files
committed
solve: 1003 피보나치 함수
1 parent 3d5ba0d commit 6d89421

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
"""
3+
문제 이름: 피보나치 함수
4+
문제 번호: 1003
5+
문제 링크: https://www.acmicpc.net/problem/1003
6+
난이도: Silver III
7+
태그: 다이나믹 프로그래밍
8+
"""
9+
import sys
10+
11+
12+
def input(): return sys.stdin.readline().rstrip()
13+
14+
15+
def solve(num: int) -> tuple:
16+
DP1 = [1, 0, 1] # 0
17+
DP2 = [0, 1, 1] # 1
18+
19+
for i in range(3, num+1):
20+
DP1.append(DP1[i-1]+DP1[i-2])
21+
DP2.append(DP2[i-1]+DP2[i-2])
22+
23+
return DP1[num], DP2[num]
24+
25+
for _ in range(int(input())):
26+
print(*solve(int(input())))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
file: "1003.md"
3+
name: "피보나치 함수"
4+
src: "https://www.acmicpc.net/problem/1003"
5+
tags:
6+
- 다이나믹 프로그래밍
7+
done: true
8+
draft: false
9+
level: 8
10+
difficulty: "Silver III"
11+
date: 2021-11-06
12+
---
13+
# 피보나치 함수
14+
15+
```text
16+
0 0
17+
1 1
18+
10 2
19+
101 3
20+
10110 4
21+
10110101 5
22+
```
23+
24+
규칙을 보자
25+
26+
```python
27+
import sys
28+
29+
def input(): return sys.stdin.readline().rstrip()
30+
31+
def solve(num: int) -> tuple:
32+
DP1 = [1, 0, 1] # 0
33+
DP2 = [0, 1, 1] # 1
34+
35+
for i in range(3, num+1):
36+
DP1.append(DP1[i-1]+DP1[i-2])
37+
DP2.append(DP2[i-1]+DP2[i-2])
38+
39+
return DP1[num], DP2[num]
40+
41+
for _ in range(int(input())):
42+
print(*solve(int(input())))
43+
```

0 commit comments

Comments
 (0)