1935(후위표기식2) : 자료구조(python)

지환·2023년 10월 28일
0

백준(python)

목록 보기
65/67

출처 | https://www.acmicpc.net/problem/1935

코드



k = int(input())
word = input()#단어들을 저장하는 공간
num_list = [0] * k


for i in range(k):
    num_list[i] = int(input())

stack = []

for i in word:

    if 'A'<=i<='Z':
       stack.append(num_list[ord(i)-ord('A')])
    
    else:
       n2 = stack.pop()
       n1 = stack.pop()
       
       if i == "+":
         stack.append(n1+n2)
        
       elif i == "-":
         stack.append(n1-n2)
        
       elif i == "/":
         stack.append(n1/n2)
        
       elif i == "*":
         stack.append(n1*n2)
    
print('%.2f' %stack[0])
          

profile
아는만큼보인다.

0개의 댓글