728x90
링크 : www.acmicpc.net/problem/14888
푸는 방법
가능한 연산의 경우를 순서대로 배열해야 되기 때문에, itertools 내부의 permutation(=순열)를 이용합니다.
(itertools 라이브러리를 이용하면 편-안하게 다양한 경우의 수를 만들 수 있습니다)
1. 연산자들을 나란히 받아서 하나의 리스트로 합친 후, 다양한 순서 개수를 따져야 하므로 순열로 변환합니다.
2. 경우의 수마다, 하나씩 순차대로 연산을 진행
3. 진행한 값을 최소, 최대값과 비교해가며 갱신한뒤, 마지막으로 출력합니다.
import itertools
N = int(input())
numbers = list(map(int, input().split()))
operators = list(map(int, input().split()))
opt_list = []
for opt, number in zip(['+', '-', '*', '/'], operators):
opt_list += opt*number
operator_combination = set(list(itertools.permutations(opt_list)))
max_value = -10**9
min_value = 10**9
for operators in operator_combination:
result = numbers[0]
for i in range(N-1):
result = int(eval(str(result)+operators[i]+str(numbers[i+1])))
max_value = max(max_value, result)
min_value = min(min_value, result)
print(max_value)
print(min_value)
728x90
'Programming Language > 알고리즘 공부' 카테고리의 다른 글
[백준] 18429 근손실 파이썬 (0) | 2021.03.29 |
---|---|
[백준] 2231 분해합 파이썬 (0) | 2021.03.29 |
[백준] 3085 사탕게임 파이썬 (0) | 2021.03.29 |
[백준] 1912 연속합 파이썬 (0) | 2021.03.29 |
[백준] 14889 스타트와 링크 파이썬 (0) | 2021.03.29 |