인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

ordinary's profile image
ordinary

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

11. Combination of Numbers (DFS)

조합결과를 인덱스로 사용하는 방법

Written on

·

311

0

안녕하세요
기존의 조합처럼 1부터 n까지의 조합결과를 구한다음에
그 값을 a 리스트의 인덱스로 사용하여 sum값을 구해도될까요?
테스트는 통과했습니다
def DFS(L, s):
global tmp, cnt
if L == k:
tmp = 0
for i in range(L):
tmp += a[res[i]-1]
if tmp % m == 0:
cnt += 1
else:
for i in range(s, n+1):
res[L] = i
DFS(L+1, i+1)

if __name__ == "__main__":
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = int(input())
tmp = 0
cnt = 0
res = [0] * (k+1)
DFS(0, 1)
print(cnt)
python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 상관없을 것 같습니다.

ordinary's profile image
ordinary

asked

Ask a question