Skip to content

Commit dc3537d

Browse files
committed
[BOJ] #14888. 연산자 끼워넣기 / 실버1 / 79분 (힌트)
1 parent bf7c42d commit dc3537d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 입력받기
2+
n = int(input()) # 수의 개수
3+
nums = list(map(int, input().split())) # 수열
4+
operator = list(map(int, input().split())) # 연산자
5+
6+
# 최솟값, 최댓값 초기화
7+
minValue= 1000000000
8+
maxValue= -1000000000
9+
10+
def calculator(idx, res, add, sub, prd, div):
11+
global minValue, maxValue
12+
13+
if idx == n: # 종료조건: 연산자를 모두 사용했을 경우
14+
# 다 탐색했기 때문에 최대최소 비교해서 최솟값과 최댓값 갱신
15+
maxValue = max(maxValue, res)
16+
minValue = min(minValue, res)
17+
return
18+
19+
if add > 0:
20+
calculator(idx + 1, res + nums[idx], add-1, sub, prd, div)
21+
22+
if sub > 0:
23+
calculator(idx + 1, res - nums[idx], add, sub-1, prd, div)
24+
25+
if prd > 0:
26+
calculator(idx + 1, res * nums[idx], add, sub, prd-1, div)
27+
28+
if div > 0:
29+
calculator(idx + 1, int(res / nums[idx]), add, sub, prd, div-1)
30+
31+
calculator(1, nums[0], operator[0], operator[1], operator[2], operator[3])
32+
print(maxValue)
33+
print(minValue)

0 commit comments

Comments
 (0)