본문 바로가기

Coding test

[백준/15655/파이썬] N과 M (6)


소스코드

n,m = list(map(int,input().split()))
seq = list(map(int,input().split()))
seq = sorted(seq)
s = []
def dfs(start):
    if len(s) == m:     
        print(' '.join(map(str,s)))
        return
    for i in seq:
      if i not in s and (i>=start):
        s.append(i)
        dfs(i)
        s.pop()
dfs(1)

알고리즘

N과 M 시리즈 중 하나로 조건을 잘 설정해주면 된다.

N과 M (5)에서 변형한 점은 and (i>=start) 이 부분인데 오름차순으로 정렬된 수열에서 앞 순번으로 삽입되는 원소들보다 크다는 조건을 넣어줘야 했다.

'Coding test' 카테고리의 다른 글

[백준/11047/파이썬] 동전  (0) 2023.02.28
[백준/18290/파이썬] NM과 K (1)  (0) 2023.02.27
[백준/15654/파이썬] N과 M (5)  (0) 2023.02.21
[백준/15652/파이썬] N과 M (4)  (0) 2023.02.20
[백준/15651/파이썬] N과 M (3)  (0) 2023.02.19