https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14tDX6AFgCFAYD
풀이 및 해석(주석)
def isp(s):
if s == '*' or s == '/':
return 2
elif s == '(':
return 0
else:
return 1
def icp(s):
if s == '*' or s == '/':
return 2
elif s == '(':
return 3
else:
return 1
def cal_1(lst): # 후위 표기식으로 변환하는 함수
stack = []
for i in lst:
if i.isdigit(): # 숫자인 경우 빈 리스트에 넣어준다.
result_1.append(i)
elif i == '(': # 여는 괄호일 경우 무조건 스택에 넣어준다.
stack.append(i)
elif i == ')': # 닫는 괄호가 나오면 여는 괄호가 나올 때 까지 pop해준다.
while stack and stack[-1] != '(':
result_1.append(stack.pop())
stack.pop() # 여는 괄호를 pop해준다.
elif i == '*' or i == '/': # 곱하기나 나누기가 나오면
if not stack: # 스택이 비어있으면 무조건 넣어주고
stack.append(i)
else: # 스택의 isp와 자기의 icp를 확인해서 isp가 낮은 연산자가 낳올 때 까지 pop해준다.
while stack and isp(stack[-1]) >= icp(i):
result_1.append(stack.pop())
stack.append(i) # pop을 다 해주고 넣어준다.
elif i == '+' or i == '-': # 더하기와 빼기도 똑같다.
if not stack:
stack.append(i)
else:
while stack and isp(stack[-1]) >= icp(i):
result_1.append(stack.pop())
stack.append(i)
while stack: # 스택이 빌 때 까지 리스트에 pop해준다.
result_1.append(stack.pop())
def cal2(lst): # 후위 표기식을 계산하는 함수
stack = []
for i in lst: # i가 숫자면 무조건 스택에 넣어준다.
if i.isdigit():
stack.append(i)
elif i == '+': # 연산자가 나오면 (스택의 두번째 pop) 연산자 (스택의 첫번째 pop) 값을 넣어준다.
a = stack.pop()
b = stack.pop()
c = int(b) + int(a)
stack.append(c)
elif i == '-':
a = stack.pop()
b = stack.pop()
c = int(b) - int(a)
stack.append(c)
elif i == '*':
a = stack.pop()
b = stack.pop()
c = int(b) * int(a)
stack.append(c)
elif i == '/':
a = stack.pop()
b = stack.pop()
c = int(b) // int(a)
stack.append(c)
return int(stack[-1])
for tc in range(1, 11):
N = int(input())
nums = list(input())
result_1 = []
cal_1(nums)
print(f'#{tc}', cal2(result_1))
후기
연산식을 입력받아 후위표기법으로 변환하는 과정과 후위표기법의 연산식을 계산하는 방법을 배웠다.
괄호가 포함된 연산식을 후위표기법으로 변환하는 과정이 까다롭긴 하지만, 막상 코드를 쓰고 나면
그렇게 어렵지는 않다.
'SWEA' 카테고리의 다른 글
SWEA 1220 Magnetic (파이썬) (0) | 2022.08.25 |
---|---|
SWEA 11315 오목 판정 (파이썬) (0) | 2022.08.25 |
SWEA 1961 숫자 배열 회전 (파이썬) (0) | 2022.08.19 |
SWEA 1945 간단한 소인수 분해 (파이썬) (0) | 2022.08.19 |
SWEA 4871 그래프 경로 DFS (파이썬) (0) | 2022.08.18 |