[python] 백준 15652번

도덩이의 개발 일지·2025년 7월 21일

백준

목록 보기
131/131
post-thumbnail

안녕하세요 !

오늘은 백준 15651번 - N과 M(3) 문제를 가져왔습니다.


문제 설명


해결 방법

문제를 해결하는 방법은 다음과 같습니다.

  1. n과 m을 입력받는다.
  2. 1부터 n까지의 요소를 포함하는 배열을 만든다.
  3. combinations_with_replacement를 사용해서 중복 조합을 만들고 양식에 맞게 출력한다.

  1. n과 m을 입력받는다.
n, m = map(int, sys.stdin.readline().split())

  1. 1부터 n까지의 요소를 포함하는 배열을 만든다.
arr = []
for i in range(1, n+1):
    arr.append(i)

  1. combinations_with_replacement를 사용해서 중복 조합을 만들고 양식에 맞게 출력한다.
for j in combinations_with_replacement(arr, m):
    for k in j:
        print(k, end=" ")
    print()

전체 코드

import sys
from itertools import combinations_with_replacement

n, m = map(int, sys.stdin.readline().split())

arr = []
for i in range(1, n+1):
    arr.append(i)

for j in combinations_with_replacement(arr, m):
    for k in j:
        print(k, end=" ")
    print()
profile
말하는 감자에서 개발자로 ( ´͈ ᵕ `͈ )◞♡

0개의 댓글