[알고리즘/백준] 14888번 : 연산자 끼워넣기(python)

유현민·2022년 3월 9일
0

알고리즘

목록 보기
43/253

순열을 이용해서 풀었다.

from itertools import permutations
N = int(input())
num = list(map(int, input().split()))
t = list(map(int, input().split()))
op_lst = ['+', '-', '*', '/']
op = []
m1 = -1e9
m2 = 1e9
for k in range(len(t)):
    for i in range(t[k]):
        op.append(op_lst[k])
oper = list(set(permutations(op,len(op))))

answer = []

answer = []
for i in oper:
    n = num[0]
    for j in range(len(num) - 1):
        if i[j] == '+':
            n += num[j + 1]
        elif i[j] == '-':
            n -= num[j + 1]
        elif i[j] == '*':
            n *= num[j + 1]
        else:
            if n // num[j + 1] < 0:
                n = -(-n // num[j + 1])
            else:
                n = n // num[j + 1]

    answer.append(n)
print(max(answer))
print(min(answer))
profile
smilegate megaport infra

0개의 댓글