[ BOJ / Python ] 15654번 N과 M (5)
이번 문제는 itertools의 permutations를 이용하여 정말 쉽게 풀 수 있었다. itertools의 permutations를 리스트로 저장한 뒤에 이를 오름차순으로 정렬하고 주어진 형식대로 출력하였다.
- n, m을 입력받는다.
- arr을 입력받는다.
- 결과를 저장할 변수 result에
sorted(list(itertools.permutations(arr, m)))
을 저장한다.
(arr에서 m개로 이뤄진 조합을 정렬된 리스트로 저장) - result의 길이만큼 반복하는 i에 대한 for문을 돌린다.
->result[i]
의 길이만큼 반복하는 j에 대한 for문을 돌린다.
--> 만약 j가result[i]
의 길이-1과 같을 경우,
--->result[i][j]
를 출력한다.
--> 그 외의 경우,
--->result[i][j]
를 출력하는데 이때 구분을 줄바꿈이 아닌 한칸의 공백으로 한다.
Code
import itertools
n, m=map(int, input().split())
arr=list(map(int, input().split()))
result=sorted(list(itertools.permutations(arr, m)))
for i in range(len(result)):
for j in range(len(result[i])):
if j==len(result[i])-1:
print(result[i][j])
else:
print(result[i][j], end=' ')
import itertools
n, m=map(int, input().split())
arr=list(map(int, input().split()))
result=sorted(list(itertools.permutations(arr, m)))
for i in range(len(result)):
for j in range(len(result[i])):
if j==len(result[i])-1:
print(result[i][j])
else:
print(result[i][j], end=' ')
Author And Source
이 문제에 관하여([ BOJ / Python ] 15654번 N과 M (5)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@xx0hn/BOJ-Python-15654번-N과-M-5저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)