Skip to content

Conversation

@3veryDay
Copy link
Contributor

@3veryDay 3veryDay commented Jan 7, 2026

🚀 이슈 번호

Resolve: {#2281}

🧩 문제 해결

스스로 해결 : ❌

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 : 정말 간단한 요청 처리 문제
  • 🔹 어떤 방식으로 접근했는지 : 단, 조금 중간에 출력 방식이 복잡함. 풀었으나 오류가 계속 발생해서 도움 받았습니다. 약 1달만에 하는 거라서 어렵네요...ㅎ ㅠㅠ

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(?)
  • 이유:

💻 구현 코드

import sys

input = sys.stdin.read

def run_stack_machine(commands, start_num):
    stack = [start_num]
    LIMIT = 10**9

    for cmd in commands:
        try:
            if cmd.startswith("NUM"):
                val = int(cmd.split()[1])
                stack.append(val)
            
            elif cmd == "POP":
                if not stack: return "ERROR"
                stack.pop()
                
            elif cmd == "INV":
                if not stack: return "ERROR"
                stack[-1] = -stack[-1]
                
            elif cmd == "DUP":
                if not stack: return "ERROR"
                stack.append(stack[-1])
                
            elif cmd == "SWP":
                if len(stack) < 2: return "ERROR"
                stack[-1], stack[-2] = stack[-2], stack[-1]

            elif cmd == "ADD":
                if len(stack) < 2: return "ERROR"
                a, b = stack.pop(), stack.pop()
                res = b + a
                if abs(res) > LIMIT: return "ERROR"
                stack.append(res)

            elif cmd == "SUB":
                if len(stack) < 2: return "ERROR"
                a, b = stack.pop(), stack.pop()
                res = b - a
                if abs(res) > LIMIT: return "ERROR"
                stack.append(res)

            elif cmd == "MUL":
                if len(stack) < 2: return "ERROR"
                a, b = stack.pop(), stack.pop()
                res = b * a
                if abs(res) > LIMIT: return "ERROR"
                stack.append(res)

            elif cmd == "DIV":
                if len(stack) < 2: return "ERROR"
                a, b = stack.pop(), stack.pop()
                if a == 0: return "ERROR"
                
                res = abs(b) // abs(a)
                if (a < 0 and b > 0) or (a > 0 and b < 0):
                    res = -res
                stack.append(res)

            elif cmd == "MOD":
                if len(stack) < 2: return "ERROR"
                a, b = stack.pop(), stack.pop()
                if a == 0: return "ERROR"
                res = abs(b) % abs(a)
                if b < 0:
                    res = -res
                stack.append(res)
        except:
            return "ERROR"

    if len(stack) == 1:
        return stack[0]
    else:
        return "ERROR"

# --- 메인 실행부 ---
data = input().split()
idx = 0

while idx < len(data):
    if data[idx] == "QUIT":
        break
    
    # 명령어 모으기
    commands = []
    while data[idx] != "END":
        if data[idx] == "NUM":
            commands.append(f"NUM {data[idx+1]}")
            idx += 2
        else:
            commands.append(data[idx])
            idx += 1
    idx += 1
    
    n = int(data[idx])
    idx += 1
    for _ in range(n):
        start_val = int(data[idx])
        idx += 1
        print(run_stack_machine(commands, start_val))
    
    print() # 기계별 구분 빈 줄

@3veryDay 3veryDay linked an issue Jan 7, 2026 that may be closed by this pull request
@3veryDay 3veryDay self-assigned this Jan 7, 2026
@alirz-pixel alirz-pixel merged commit 435b982 into main Jan 12, 2026
2 checks passed
@alirz-pixel alirz-pixel deleted the 현서/2281/1 branch January 12, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260107 : 코딩테스트

3 participants