Part5.11_완전탐색_부분집합 구하기(DFS)_수들의 조합

Eugenius1st·2022년 1월 26일
0

Python_algorithm

목록 보기
39/83

수들의 조합

내가 생각한 코드

import sys
sys.stdin = open("input.txt", "rt")

def DFS(L, s, sum):
    global cnt
    if L == m and int(sum%standard)==0:
        cnt+=1
    if L > m:
        return
    else:
        for i in range(s, n): #n은 4
            DFS(L+1, i+1, sum+list[i]) #레벨 1 증가, 뻗는 가지 1 증가!!! 가지는 i 인거 명심... 여기서 오래 걸림..
                 

if __name__ == "__main__":
    n, m = map(int,input().split()) # 5 3
    list = list(map(int,input().split()))
    standard = int(input())
    cnt = 0
    DFS(0, 0, 0)
    print(cnt)

잘 풀긴 했지만.. 가지(s) 가 s+1인줄 알았다... 사실은 i+1이 되는 것이지 !!
가지는 규칙적으로 유동적으로 움직이는 값이므로..

for문 돌면서 커져야 하기 때문에..!!

선생님 코드

import sys
sys.stdin = open("input.txt", "rt")

def DFS(L, s, sum):
    global cnt
    if L == k :
        if sum%m ==0:
            cnt+=1
    else:
        for i in range(s, n): #n은 4
            DFS(L+1, i+1, sum+a[i])
if __name__ == "__main__":
    n, k = map(int,input().split()) # 5 3
    a = list(map(int,input().split()))
    m = int(input())
    cnt = 0
    DFS(0, 0, 0)
    print(cnt)

똑같당

profile
최강 프론트엔드 개발자가 되고싶은 안유진 입니다

0개의 댓글