24-Summer 2회차 모각코

YEEUN CHOI·2024년 7월 5일

목표

2회차: 24/07/05 11:00 ~ 14:00
장소: ZOOM
계획: 24년 하계 모각코

스터디 계획

스터디 주제 : 스택 자료구조를 활용하여 중위 표기식을 후위 표기식으로 변환하기
https://www.acmicpc.net/problem/1918

스터디 목표 : 백준 알고리즘 문제를 미리 선정하여 해결하기

결과

문제


문제풀이

num = input()
answer = ''
stack = []

for i in num:
    if i.isalpha():
        answer += i
    else:
        if i == '(':
            stack.append(i)
            
        elif i == '*' or i == '/':
            while stack and (stack[-1] == '*' or stack[-1] == '/'):
                answer += stack.pop()
            stack.append(i)
            
        elif i == '+' or i == '-':
            while stack and stack[-1] != '(':
                answer += stack.pop()
            stack.append(i)
            
        elif i == ')':
            while stack and stack[-1] != '(':
                answer += stack.pop()
            stack.pop()

while stack:
    answer += stack.pop()

print(answer)

활동사진

개인 블로그 링크

0개의 댓글