백준 1935 후위 표기식2

김민영·2023년 1월 6일
0

알고리즘

목록 보기
31/125

과정

  • 후위 표기식을 계산하는 문제
  • 스택
  • 문자와 숫자를 연결하는 딕셔너리 만들기
  • 숫자 스택과 기호 스택을 따로 만들기
  • 입력받는 문자열을 앞에서부터 순서대로 순회
    • 숫자면 숫자 스택에 입력
    • 기호면 숫자 스택의 맨 뒤 두 숫자를 연산, 연산 결과를 push
  • 소수점 둘째 자리까지 출력
    • f-string 방식으로 출력하기
    • print(f'{변수명:.2f}')
N = int(input())
lst = list(input())
oper_lst = ["+", "-", "/", "*"]
num_lst = []

num_dict = {}
for i in range(N):
    num_dict[chr(65+i)] = int(input())

for i in lst:
    if i not in oper_lst:
        num_lst.append(num_dict[i])
    if i in oper_lst:
        b = num_lst.pop(-1)
        a = num_lst.pop(-1)
        if i == "+":
            num_lst.append(a+b)
        elif i == "-":
            num_lst.append(a-b)
        elif i == "*":
            num_lst.append(a*b)
        elif i == "/":
            num_lst.append(a/b)

print(f'{num_lst[0]:.2f}')
  • 출력 방식 기억해두기
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글