Coding Test/baekjoon

[백준 10974] 모든 순열 - python (solved.ac - 실버 3)

조용장 2021. 9. 11. 23:04

풀이

문제를 보았을때 알수있는 힌트

문제를 통해 순열을 출력하는 문제

  1. 1부터 N까지의 수를 출력
  2. 사전순으로 출력

이렇게 2가지를 조심하면서 문제를 풀어야할듯하다

 

문제를 풀기 위한 조건 정의

이 문제를 풀기 위해서 python에서는 itertools 라이브러리를 활용하면 쉽게 문제를 풀수있다.

 

itertools 라이브러리에 permutations 함수를 불러오면 순열을 불러오는 것은 끝이난다.

 

[1,2,3]을 가지고있는 순열을 뽑아보면

이런식으로 출력되는 것을 알 수 있다.

 

이를 이용해서 문제를 풀면

import sys
import itertools

def solution(mylist):
    number= [x for x in range(1,mylist+1)]
    answer= list(map(list, itertools.permutations(number)))
    answer.sort()
    return answer

n = int(input())
results = solution(n)

for i in results:
    for j in i:
        print(j,end=" ")
    print("")

이렇게 코드를 짤수있다.

 

결과

전체 코드 공유

https://github.com/dydwkd486/coding_test/blob/main/baekjoon/baekjoon10974.py