문제 링크: https://www.acmicpc.net/problem/15658
백준 알고리즘 중급 1/3 531에서 5번째 - 15658번 연산자 끼워넣기 (2)를 풀어보았다.
풀이: 연산자 끼워넣기 코드를 그대로 쓰면 풀린다.
C++
Python
def dfs(dep,tot,pl,mi,mu,di):
global maxs,mins
if dep==n:
maxs=max(tot,maxs)
mins=min(tot,mins)
return
if pl:
dfs(dep+1,tot+nums[dep],pl-1,mi,mu,di)
if mi:
dfs(dep+1,tot-nums[dep],pl,mi-1,mu,di)
if mu:
dfs(dep+1,tot*nums[dep],pl,mi,mu-1,di)
if di:
dfs(dep+1,int(tot/nums[dep]),pl,mi,mu,di-1)
n=int(input())
nums=list(map(int,input().split()))
p,s,m,d=map(int,input().split())
maxs=-1e9
mins=1e9
dfs(1,nums[0],p,s,m,d)
print(maxs)
print(mins)
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
에너지 모으기 풀이 (0) | 2023.02.16 |
---|---|
두 동전 풀이 (0) | 2023.02.16 |
14225번 부분수열의 합 풀이 (0) | 2023.02.16 |
연산자 끼워넣기 풀이 (0) | 2023.02.16 |
단어 수학 풀이 (0) | 2023.02.16 |