코딩 테스트/백준 강의 연습편

브루트포스) 15659.연산자 끼워넣기(3)

Carnival7 2022. 6. 7. 18:24

https://www.acmicpc.net/problem/15659

 

15659번: 연산자 끼워넣기 (3)

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

www.acmicpc.net

n=int(input())

nums=list(map(int,input().split()))

ops = list(map(int,input().split()))

max_ans=-1000000001
min_ans=10000000001

def dfs(index:int,exp:str):
    global max_ans,min_ans
    # 종료 조건
    if index==n:
        max_ans = max(eval(exp),max_ans)
        min_ans = min(eval(exp),min_ans)
        return
    if index>n:
        return

    # 더하기
    if ops[0] > 0:
        ops[0]-=1
        dfs(index+1,exp+"+"+str(nums[index]))
        ops[0]+=1
    # 빼기
    if ops[1] > 0:
        ops[1]-=1
        dfs(index+1,exp+"-"+str(nums[index]))
        ops[1]+=1
    # 곱하기
    if ops[2] >0:
        ops[2]-=1
        dfs(index+1,exp+"*"+str(nums[index]))
        ops[2]+=1
    # 나누기
    if ops[3]>0 :
        ops[3]-=1
        dfs(index+1,exp+"//"+str(nums[index]))
        ops[3]+=1

dfs(1,str(nums[0]))

print(max_ans)
print(min_ans)