코딩테스트
[Softeer] Level3 문제풀이 by Python
하루살이
2023. 2. 4. 03:16
1. 택배 마스터 광우- 순열(중복고려)이용
import sys
from itertools import permutations
def sol(li):
t = 0
total = 0
for i in range(k):
check = 0
while True:
check += li[t]
t = (t+1) % n
if (check+li[t]) > m:
break
total += check
return total
n, m, k = map(int, sys.stdin.readline().split())
li = list(map(int, sys.stdin.readline().split()))
arr_s = list(permutations(li, n)) # 순열(순서고려)
answer = []
for arr in arr_s:
answer.append(sol(arr)) # 각 경우마다 결과 값 넣기
print(min(answer)) # 결과 값 중에 가장 작은 값 출력
# permutations: 순열 = nCr => 순서고려
# combinations: 조합 = nPr => 순서고려 X
# product: 중복허용 순열
# combinations_with_replacement: 중복허용 조합
# 1) 순열
from itertools import permutations
data = ['A', 'B', 'C']
result = list(permutations(data, 3)) # 3개 뽑는 모든 순열 구하기
print(result)
# 2) 조합
from itertools import combinations
data = ['A', 'B', 'C']
result = list(combinations(data, 2)) # 3개 뽑는 모든 순열 구하기
print(result)
# 3) 중복순열
from itertools import product
data = ['A', 'B', 'C']
result = list(product(data, repeat=2)) # 2개 뽑는 중복순열 구하기
print(result)
# 4) 중복조합
from itertools import combinations_with_replacement
data = ['A', 'B', 'C']
result = list(combinations_with_replacement(data, 2)) # 2개 뽑는 중복조합 구하기
print(result)