알고리즘/백준

[백준] 14888번 연산자 끼워넣기 (파이썬)

알감자 2021. 12. 29. 21:33
 

14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 

www.acmicpc.net

 

코드

n = int(input())
num_list = list(map(int, input().split()))
operate_list = list(map(int, input().split()))

def operate_list_dict(operate_list):
    dictionaly = dict()
    for key, value in enumerate(operate_list):
        dictionaly[key] = value
    return dictionaly

def num_calculate(init_num,after_num,key):
    if key == 0:
        return init_num + after_num
    elif key == 1:
        return init_num - after_num
    elif key == 2:
        return init_num * after_num
    elif key == 3:
        if init_num < 0:
            init_num = -1 * init_num
            return (init_num//after_num) * -1
        else:
            return (init_num//after_num)

diction = operate_list_dict(operate_list)

arr = []
def main_calculate(num_list, diction):
    if len(num_list) == 1:
        arr.append(num_list[0])
        return

    else:
        init_number = num_list.pop(0)
        after_number = num_list[0]

        for key, value in diction.items():
            if value == 0:
                continue
            num = num_calculate(init_number, after_number, key)
            diction[key] = diction[key] -1


            deliver_list = [num] + num_list[1:]

            main_calculate(deliver_list,diction)
            diction[key] = diction[key] +1

main_calculate(num_list, diction)
print(max(arr))
print(min(arr))