방명록
- 백준(BAEKJOON) 1918 후위표기식 - Python2023년 12월 26일 18시 44분 20초에 업로드 된 글입니다.작성자: Moonsu99
https://www.acmicpc.net/problem/1918
1918번: 후위 표기식
첫째 줄에 중위 표기식이 주어진다. 단 이 수식의 피연산자는 알파벳 대문자로 이루어지며 수식에서 한 번씩만 등장한다. 그리고 -A+B와 같이 -가 가장 앞에 오거나 AB와 같이 *가 생략되는 등의
www.acmicpc.net
풀이
n = input() list_operator = [] # 연산자 저장 result = [] # 결과 for i in n: if i == '(': # 열괄 list_operator.append(i) # 연산자 저장 elif i == ')': # 닫괄 # 연산자 스택이 비어 있지 않고, 여는 괄호를 만날 때까지 while list_operator and list_operator[-1] != '(': result.append(list_operator.pop()) # 연산자 스택에서 pop하여 결과 리스트에 추가 if list_operator: # 여는 괄호를 만나면 list_operator.pop() # 연산자 스택에서 제거 해보리기 elif i in ['*', '/']: # 연산자 스택의 top이 '*' 또는 '/'일 경우 while list_operator and list_operator[-1] in ['*', '/']: result.append(list_operator.pop()) # 연산자 스택에서 pop하여 결과 리스트에 추가 list_operator.append(i) elif i in ['+', '-']: while list_operator and list_operator[-1] != '(': result.append(list_operator.pop()) list_operator.append(i) else: result.append(i) # 입력 식을 모두 처리한 후 남은 연산자를 처리 while list_operator: result.append(list_operator.pop()) # 연산자 스택이 비어질 때까지 pop하여 결과 리스트에 추가 print(''.join(result))
'코딩테스트 > 백준' 카테고리의 다른 글
[프로그래머스 LV2] 튜플 - Python (0) 2024.03.28 백준(BAEKJOON) 22942 데이터 체커 - Python (1) 2023.12.26 백준(BAEKJOON) 2800 괄호제거 - Python (1) 2023.12.03 백준(BAEKJOON) 2504 괄호의 값 - Python (0) 2023.12.01 백준(BAEKJOON) 1935 후위 표기식2 - Python (0) 2023.11.24 다음글이 없습니다.이전글이 없습니다.댓글