본문 바로가기
CS/알고리즘

[카테부] 코테스터디 2주차 - N과 M(5) (백준 15654)

by alphaca202 2024. 8. 13.

순열 문제다 ! n개 중에 m개 골라 나열하라 

순서 O, 중복 X 문제임. 

# 순열 - n개 중에 m개 골라 나열하기

n, m = map(int, input().split())
arr = list(map(int, input().split()))
visited = [0] * n
arr.sort()

ans = []

def print_ans():
    for a in ans:
        print(arr[a], end=" ")
    print()

def choose(cnt):
    if cnt > m:
        print_ans()
        return
    
    for i in range(0, n):
        if visited[i] == 0:
            ans.append(i)
            visited[i] = 1
            choose(cnt+1)
            ans.pop()
            visited[i] = 0

choose(1)

 

 

전에 정리해 놨던 순열 조합 정리본이다! 

https://alphaca202.tistory.com/11

 

[알고리즘] 백트래킹 - 경우의 수 문제

원하는 조합과 순열 만드는 방법1. for문으로 구현하는 방법과2. 재귀함수로 구현하는 방법!  원하는 숫자의 조합이나 순열을 만들고, 객체를 선택하는 방법이다! 경우의수 문제는 중복순열순

alphaca202.tistory.com