Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 관계형 데이터베이스
- UUID
- 중복되지 않는 값 만들기
- 공백검사
- Kafka
- ORM
- auto-offset-reset
- 스프링 시큐리티
- msa
- 게시판 작성자를 아이디로
- springboot
- 자바
- Thymeleaf
- JPA
- 자바 ORM 표준 JPA 프로그래밍
- 모던 자바스크립트 Deep Dive
- 유효성검사
- java
- 출처 모던 자바스크립트 Deep Dive
- spring security
- 자바스크립트
Archives
- Today
- Total
인지용
[백준] 1935 후위 표기식2 [python, 실버3, 스택] 본문
https://www.acmicpc.net/problem/1935
import sys
import operator
def input():
return sys.stdin.readline().strip()
n = int(input())
text = list(input())
stack = []
nums = []
ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv
}
for i in range(n):
nums.append(int(input()))
for i in text:
if i.isalpha():
index = ord(i) - ord('A')
stack.append(nums[index])
else:
a = stack.pop()
b = stack.pop()
stack.append(ops[i](b, a))
print("{:.2f}".format(stack.pop()))
입력이 문자열이면 스택에 넣다가
후위표기식이 나오면 스택 마지막 2개를 빼서 계산하고 결과를 다시 스택에 넣으면 된다.
핵심은 문자열과 숫자를 잘 매핑해놓고 재활용해서 계산하면 된다.
문자열이라고해서 신규 입력을 받으면 실패.. (내가 처음에 그랬음 ㅎ)
'알고리즘' 카테고리의 다른 글
| [백준] 4889 안정적인 문자열 [python, 실버1, 스택] (0) | 2026.01.19 |
|---|---|
| [백준] 2841 외계인의 기타 연주 [python, 실버1, 스택] (0) | 2026.01.16 |
| [백준] 12789 도키도키 간식드리미 [python, 실버2, 스택] (0) | 2026.01.14 |
| 백준 11656 : 접미사 배열 자바 (0) | 2021.09.20 |
| 백준 1463번: 1로 만들기 자바 (0) | 2021.09.12 |
