Skip to content

Commit

Permalink
2024-04-24 22:06:50
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/boj-1935-후위-표기식2.md
  • Loading branch information
gyunseo committed Apr 24, 2024
1 parent 6f3c954 commit d92df48
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
30 changes: 24 additions & 6 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
{
"id": "e86c6645d916ff37",
"type": "tabs",
"children": [
{
"id": "eead2d944368253b",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/boj-1935-후위-표기식2.md",
"mode": "source",
"source": false
}
}
}
]
},
{
"id": "1ead376bb812a34b",
"type": "tabs",
"children": [
{
"id": "9291886fba53397c",
Expand Down Expand Up @@ -85,7 +103,7 @@
"state": {
"type": "backlink",
"state": {
"file": "src/content/blog/boj-10819-차이를-최대로.md",
"file": "src/content/blog/boj-1935-후위-표기식2.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -102,7 +120,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "src/content/blog/boj-10819-차이를-최대로.md",
"file": "src/content/blog/boj-1935-후위-표기식2.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -125,7 +143,7 @@
"state": {
"type": "outline",
"state": {
"file": "src/content/blog/boj-10819-차이를-최대로.md"
"file": "src/content/blog/boj-1935-후위-표기식2.md"
}
}
}
Expand All @@ -148,10 +166,11 @@
"table-editor-obsidian:Advanced Tables Toolbar": false
}
},
"active": "9291886fba53397c",
"active": "eead2d944368253b",
"lastOpenFiles": [
"src/content/blog/boj-1026-보물.md",
"src/content/blog/boj-10819-차이를-최대로.md",
"src/content/blog/boj-1935-후위-표기식2.md",
"src/content/blog/boj-1026-보물.md",
"src/content/blog/programmers-보호소에서-중성화한-동물.md",
"src/content/blog/programmers-즐겨찾기가-가장-많은 식당-정보-출력하기.md",
"src/assets/Screenshot 2024-04-23 at 01.28.02.png",
Expand All @@ -177,7 +196,6 @@
"src/content/blog/bfs-algorithm-technique.md",
"src/content/blog/go-study.md",
"src/content/blog/dfs-and-bfs-algorithms.md",
"src/content/blog/0-1-knapsack-dp.md",
"dist/assets/[email protected]",
"dist/assets/forrest-gump-quote.webp.jpg",
"dist/_astro/[email protected]",
Expand Down
77 changes: 77 additions & 0 deletions src/content/blog/boj-1935-후위-표기식2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
author: Gyunseo Lee
title: "BOJ 백준 1935: 후위 표기식2"
pubDatetime: 2024-04-24T22:00:00+09:00
modDatetime: 2024-04-24T22:00:00+09:00
featured: false
draft: false
tags:
- PS
- Algorithms
- BOJ
- Stack
- 실랜디
description: "\b백준 1935: 후위 표기식2 풀이 과정"
ogImage: ""
---

## Table of contents

## 들어가며

이 문제는 후위 표기식으로 표현된 문자열을 연산해서 그 결과값을 출력하는 문제입니다.
중위 표기식은 인간이 평소에 쓰는 수학식이고, 전위 표기식일 경우에는 계산 결과값 스택과 연산자 스택을 두고 계산 결과값 스택의 길이가 2이상될 때마다 트리거 시켜서 연산자 스택에서 연산자 하나 꺼내서 계산을 하면 됩니다.
후위 표기식도 똑같은 논리로 계산하면 됩니다.

## 풀이 과정

![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1713963958/image_owpqsx.png)

그냥 전위 표기식을 스택을 이용해서 계산하듯이, 똑같이 짜면 됩니다.

## AC 받은 Python 코드

```python
import sys

input = sys.stdin.readline


def is_alphabet(ch):
if ch >= "A" and ch <= "Z":
return True
if ch >= "a" and ch <= "z":
return True
return False


if __name__ == "__main__":
N = int(input().rstrip())
post = input().rstrip()
alphabet = {}
operation = {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"*": lambda x, y: x * y,
"/": lambda x, y: x / y,
}
for i in range(N):
alphabet[chr(ord("A") + i)] = int(input().rstrip())

calcStack = []
operatorStack = []
for ch in post:

if is_alphabet(ch):
calcStack.append(alphabet[ch])
else:
operatorStack.append(ch)

if len(calcStack) >= 2 and operatorStack:
x, y = calcStack[-2], calcStack[-1]
del calcStack[-2:]
calcStack.append(operation[operatorStack.pop()](x, y))

print(f"{calcStack[-1]:0.2f}")

```

0 comments on commit d92df48

Please sign in to comment.