https://github.com/cjk09083/Code
https://www.acmicpc.net/problem/9095
9095번: 1, 2, 3 더하기
각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 출력한다.
www.acmicpc.net
해답 1)
'''
- DFS를 사용해 전개
- 48ms
'''
import sys
input = sys.stdin.readline
def dfs(depth, val, left):
global ans
if left < 0:
return
elif left == 0:
print(f'd: {depth}, v: {val}, l: {left} COMP')
ans += 1
return
elif depth > n:
return
else:
for i in range(1, 4):
new_val = val.copy()
new_val.append(i)
# print(f'd: {depth}, v: {new_val}, l: {left}, i:{i}')
dfs(depth+1, new_val, left-i)
return
if __name__ == "__main__":
t = int(input())
result = []
for _ in range(t):
n = int(input())
ans = 0
for i in range(1, 4):
val = [i]
depth = 0
left = n
# print(f'd: {depth}, v: {val}, l: {left}, i:{i}')
dfs(depth+1, val, left-i)
result.append(ans)
print(*result, sep = "\n")
해답 2)
'''
- DP를 사용해 미리 답을 구해놓고 출력
- 36ms
'''
import sys
input = sys.stdin.readline
if __name__ == "__main__":
t = int(input())
MAX = 11
c = [0] * MAX
c[1] = 1
c[2] = 2
c[3] = 4
for i in range(4, MAX):
c[i] = c[i-1] + c[i-2] + c[i-3]
for _ in range(t):
n = int(input())
print(c[n])
'미래의 나를 위한 메모 > 코딩테스트' 카테고리의 다른 글
[230216] Chat GPT로 코테 풀이 개선하기 (백준 14500 python) (0) | 2023.02.16 |
---|---|
[230125] 백준 15649번 - N과 M (1) (Python) (0) | 2023.01.25 |
[230119] 백준 1748번 - 수 이어 쓰기 1 (Python) (0) | 2023.01.19 |
[230117] 백준 6064번 - 카잉달력 (Python) (0) | 2023.01.17 |
[230116] 백준 14500번 - 테트로미노 (Python) (2) | 2023.01.16 |