b_연산자 끼워넣기_14888

source www.acmicpc.net/problem/14888
topics
types 문제풀이
정답여부 성공

문제

입력
수개수
수 배열
덧셈 뺄셈 곱셈 나눗샘 개수

전형적인 백트래킹문제다.

import sys
import math

k = int(sys.stdin.readline().strip())
arr = list(map(int,sys.stdin.readline().strip().split()))
oper= list(map(int,sys.stdin.readline().strip().split()))
max_v = -math.inf
min_v = math.inf

# 1, 첫숫자로시작
def solve(step,sum_v,oper):
    global max_v
    global min_v
    # print(step,sum_v,oper,max_v,min_v)
    if step >= len(arr):
        # print("탈출",sum_v)
        if max_v< sum_v:
            max_v = int(sum_v)
        if min_v > sum_v:
            min_v = int(sum_v)
        # print(max_v,min_v)
        return
    for i in range(len(oper)):
        if oper[i] <= 0 : continue
        oper[i] -=1
        if i <mark> 0 :
            solve(step+1,sum_v+arr[step],oper)
        elif i </mark> 1:
            solve(step+1,sum_v-arr[step],oper)
        elif i <mark> 2:
            solve(step+1,sum_v*arr[step],oper)
        elif i </mark> 3:
            tmp = 0
            if sum_v < 0:
                tmp = -((-sum_v)//arr[step])
            else:
                tmp = (sum_v)//arr[step]
            solve(step+1,tmp,oper)
        oper[i] +=1

solve(1,arr[0],oper)
print(max_v)
print(min_v)