https://www.acmicpc.net/problem/1935
1935번: 후위 표기식2
첫째 줄에 피연산자의 개수(1 ≤ N ≤ 26) 가 주어진다. 그리고 둘째 줄에는 후위 표기식이 주어진다. (여기서 피연산자는 A~Z의 영대문자이며, A부터 순서대로 N개의 영대문자만이 사용되며, 길이
www.acmicpc.net
import sys
n = int(sys.stdin.readline().strip())
exp = sys.stdin.readline().strip()
d = {}
stack = []
for i in range(n):
d[chr(65+i)] = int(sys.stdin.readline().strip())
for c in exp:
if c.isalpha(): # 값이면
stack.append(d[c])
else: # 연산자면
num2 = stack.pop()
num1 = stack.pop()
if c == "+":
stack.append(num1+num2)
elif c == "-":
stack.append(num1-num2)
elif c == "*":
stack.append(num1*num2)
elif c == "/":
stack.append(num1/num2)
print(f'{stack.pop():.2f}')
'알고리즘 > 백준-파이썬' 카테고리의 다른 글
[백준] 10809번 (python 파이썬) (0) | 2022.04.09 |
---|---|
[백준] 10808번 (python 파이썬) (0) | 2022.04.08 |
[백준] 1918번 (python 파이썬) (0) | 2022.04.08 |
[백준] 17299번 (python 파이썬) (0) | 2022.04.07 |
[백준] 17298번 (python 파이선) (0) | 2022.04.06 |