본문 바로가기
Algorithm/Brute force

[백준] 14888번 연산자 끼워넣기

by 등촌동 꼬북이 2021. 2. 5.

from itertools import permuitations를 사용하면 쉽게 풀 수 있다.

 

from itertools import permutations
import copy
import sys
input()
minVal = 1000000000
maxVal = -1000000000
dataList = list(map(int, sys.stdin.readline().split()))
methodList = list(map(int, sys.stdin.readline().split()))
methodStr = ""
methodStr += "+" * methodList[0]
methodStr += "-" * methodList[1]
methodStr += "*" * methodList[2]
methodStr += "/" * methodList[3]

for operation in set(permutations(methodStr, len(methodStr))):
    # 계산 로직
    tempList = copy.deepcopy(dataList)
    calVal = tempList.pop(0)
    for idx, op in enumerate(operation):
        if op == "+":
            calVal += tempList[idx]
        elif op == "-":
            calVal -= tempList[idx]
        elif op == "*":
            calVal *= tempList[idx]
        else:
            if calVal < 0:
                calVal = -(calVal)
                calVal //= tempList[idx]
                calVal = -(calVal)
            else:
                calVal //= tempList[idx]
    maxVal = max(maxVal, calVal)
    minVal = min(minVal, calVal)

print(maxVal)
print(minVal)

'Algorithm > Brute force' 카테고리의 다른 글

[백준] 10819번 차이를 최대로  (0) 2021.02.05
[백준] 1182 부분수열의 합  (0) 2021.02.04
[백준] 2309번 일곱 난쟁이  (0) 2021.02.04
[백준] 2798 블랙잭  (0) 2021.02.04

댓글