순열과 조합
순열과 조합
사용법
연습 문제
BOJ 10974
BOJ 2798
풀어볼 문제
중복 조합
Last updated
Last updated
from itertools import permutations as pm # 순열
from itertools import combinations as cb # 조합# 순열
from itertools import permutations as pm
a = [1, 2, 3, 4]
for t in pm(a, 2):
print(t)
# (1, 2)
# (1, 3)
# (1, 4)
# (2, 1)
# (2, 3)
# (2, 4)
# (3, 1)
# (3, 2)
# (3, 4)
# (4, 1)
# (4, 2)
# (4, 3)
# 혹은 길이가 2이므로 변수를 2개로 해서 각각 출력할 수 있음
for x, y in pm(a,2):
print(x, y)
# 출력은 위에 괄호만 제거하면 같음#조합
from itertools import combinations as cb
a = [4, 3, 2, 1]
for t in cb(a, 2):
print(t)
# (4, 3)
# (4, 2)
# (4, 1)
# (3, 2)
# (3, 1)
# (2, 1)
# 조합도 마찬가지로 길이가 2이므로 for문에 변수를 2개 넣어 각각 출력 가능
for x, y in cb(a, 2):
print(x, y)# 방법 1
from itertools import permutations as pm
n = int(input())
arr = [i for i in range(1, n+1)]
for t in pm(arr, n):
print(' '.join(map(str,t))) # 튜플안의 값이 int이므로 str로 바꿈
# 방법 2
from itertools import permutations as pm
n = int(input())
arr = [i for i in range(n, 0, -1)] # 배열을 아무렇게나 넣음
ans = list(pm(arr, n))
ans.sort()
for i in range(len(ans)):
print(' '.join(map(str,ans[i])))from itertools import combinations as cb
n, m = map(int,input().split())
arr = list(map(int,input().split()))from itertools import combinations as cb
n, m = map(int,input().split())
arr = list(map(int,input().split()))
ans = 0
for t in cb(arr, 3):
if sum(t) <= m:
ans = max(ans, sum(t))
print(ans)# 중복 조합
from itertools import combination_with_replacement as cbwr
for arr in cbwr([1, 2, 3, 4], 2): #cbwr(선택할 배열, 뽑는 갯수)
print(arr)
# 중복을 포함하여 [1, 2, 3, 4]에서 원소 2개를 포함하여 출력n, m = map(int, input().split())n, m = map(int, input().split())
arr = [i for i in range(1, n+1)]from itertools import combinations_with_replacement as cbwr
n, m = map(int, input().split())
arr = [i for i in range(1, n+1)]
for x in cbwr(arr, m):
print(' '.join(map(str, x)))