๐ŸŒฑ → ๐ŸŒณ

[๋ฐฑ์ค€] 10828๋ฒˆ: ์Šคํƒ - python ๋ณธ๋ฌธ

Algorithms

[๋ฐฑ์ค€] 10828๋ฒˆ: ์Šคํƒ - python

BAY 2023. 1. 1. 19:04
728x90

https://www.acmicpc.net/problem/10828

 

10828๋ฒˆ: ์Šคํƒ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

 

stack class๋ฅผ ์ •์˜ํ•˜์—ฌ stack์˜ ๊ธฐ๋Šฅ/๋ช…๋ น์„ ๊ตฌํ˜„ํ•จ 

sys๋ฅผ ์“ฐ์ง€ ์•Š์œผ๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์Œ import sys๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ ํ•ด๊ฒฐ 

 

์ •๋‹ต์ฝ”๋“œ:

import sys
input = sys.stdin.readline

class Stack:
  def __init__(self):
    self.data=[]
  def empty(self):
    if len(self.data) == 0:
      return 1
    else:
      return 0
  def size(self):
    return len(self.data)
  def push(self, X):
    self.data.append(X)
  def pop(self):
    if self.size() == 0:
      return -1
    else:
      return self.data.pop()
  def top(self):
    if self.size() == 0:
      return -1
    else:
      return self.data[-1]

num = int(input())
stack = Stack()
for _ in range(num):
  cmd = input().split()

  op = cmd[0]
  if op == 'push':
    stack.push(cmd[1])
  elif op == 'empty':
    print(stack.empty())
  elif op == 'pop':
    print(stack.pop())
  elif op == 'size':
    print(stack.size())
  elif op == 'top':
    print(stack.top())
  else:
    print('err')
728x90