문제 링크: https://www.acmicpc.net/problem/1918
백준 알고리즘 기초 1/2 203에서 2번째 - 1918번 후위 표기식를 풀어보았다.
풀이: https://island-developer.tistory.com/83 이 코드를 참고했다.
C++
Python
inp=input()
stack=[]
res=""
for i in inp:
if i.isalpha(): res+=i
else:
if i=='(':
stack.append(i)
elif i=='*' or i=='/':
while stack and (stack[-1]=='*'or stack[-1]=='/'):
res+=stack.pop()
stack.append(i)
elif i=='+' or i=='-':
while stack and stack[-1]!='(':
res+=stack.pop()
stack.append(i)
elif i==')':
while stack and stack[-1]!='(':
res+=stack.pop()
stack.pop()
while stack:
res+=stack.pop()
print(res)
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
문자열 분석 풀이 (0) | 2023.01.10 |
---|---|
알파벳 개수 풀이 (0) | 2023.01.10 |
후위 표기식2 풀이 (0) | 2023.01.10 |
오등큰수 풀이 (0) | 2023.01.10 |
오큰수 풀이 (0) | 2023.01.10 |