Bamboo is coming

[211109] 백준 1935 후위표기식2 실버3 문제풀이 python 본문

PS

[211109] 백준 1935 후위표기식2 실버3 문제풀이 python

twenty 2022. 1. 14. 00:55

 

- 알고리즘 분류 : 자료구조, 스택

 


 

 

#백준 1935 후위표기식2 실버3 스택

import sys
input = sys.stdin.readline

stack = []
N=int(input()) #피연산자 개수 N
txt = input() #후위표기식
opr = []
for i in range(N):
    opr.append(input().strip())

for i in txt.strip():
    if(i>="A" and i<="Z"):
        stack.append(int(opr[ord(i)-65])) #아스키 코드 이용
    else:
        y = stack.pop()
        x = stack.pop()
        if(i=='+'):
            ans = x+y
        elif(i=='-'):
            ans = x-y
        elif(i=='*'):
            ans = x*y
        elif(i=='/'):
            ans = x/y
        stack.append(ans)

print("%.2f" % stack.pop())
 

 

 

 

- 파이썬에서 stack은 list 구조로 구현하기

 

 


 

 

 

 

Comments