문제
코드
from itertools import permutations
from collections import deque
import sys
input = sys.stdin.readline
def solve(n: int, a_list: list, operators: list) -> str:
max_value, min_value = -1e9, 1e9
queue = deque(list(permutations(operators, len(operators))))
while queue:
now = queue.popleft()
num = a_list[0]
for x in range(1, n):
if now[x-1] == '+':
num += a_list[x]
elif now[x-1] == '-':
num -= a_list[x]
elif now[x-1] == '*':
num *= a_list[x]
elif now[x-1] == '÷':
num = num // a_list[x] if num >= 0 else (abs(num) // a_list[x]) * -1
max_value, min_value = max(max_value, num), min(min_value, num)
return '\n'.join(map(str, [max_value, min_value]))
if __name__ == '__main__':
n = int(input())
a_list = list(map(int, input().split()))
operator_nums = list(map(int, input().split()))
operator, operators = ['+', '-', '*', '÷'], []
for x in range(4):
if operator_nums[x]:
operators.extend(operator[x] * operator_nums[x])
print(solve(n, a_list, operators))
결과
출처 & 깃허브
boj 14888
github