DevYoon
[SWEA] 4008. ์ซ์ ๋ง๋ค๊ธฐ (Python) ๋ณธ๋ฌธ
link ๐ https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeRZV6kBUDFAVH
1๏ธโฃ DFS๋ก ๋์ฌ ์ ์๋ ์ฐ์ฐ์ ์กฐํฉ ๊ตฌํด์ฃผ๊ธฐ
2๏ธโฃ calculate() ํจ์๋ก ์ซ์+์ฐ์ฐ์, ์ผ์ชฝ → ์ค๋ฅธ์ชฝ ์์๋ก ๊ณ์ฐ
3๏ธโฃ Max, Min๊ณผ ๋น๊ตํ์ฌ ๊ฐฑ์
t = int(input())
def calculate(operator):
global Max, Min
temp = num[0]
for i in range(1, N):
if operator[i-1] == '+':
temp += num[i]
elif operator[i-1] == '-':
temp -= num[i]
elif operator[i-1] == '*':
temp *= num[i]
else:
temp /= num[i]
temp = int(temp)
if Max < temp:
Max = temp
if Min > temp:
Min = temp
def dfs(level, operator):
global cal
if level == limit:
calculate(operator)
return
for i in range(4):
if cal[i] > 0:
cal[i] -= 1
dfs(level+1, operator+ope[i])
cal[i] += 1
for tc in range(1, t+1):
N = int(input())
cal = list(map(int, input().split()))
num = list(map(int, input().split()))
ope = ['+', '-', '*', '/']
limit = sum(cal)
Max = -1e8
Min = 1e8
dfs(0, '') # level, ์ฐ์ฐ์
print(f'#{tc} {Max-Min}')