题解 | #【模板】栈#
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
- 注意输入是一行一行
input
class stack(object): def __init__(self): self.stack = list() self.p = 0 def push(self, num): self.stack.append(num) self.p += 1 def top(self): if self.p > 0: print(self.stack[-1]) return print('error') def pop(self): if self.p > 0: self.p -= 1 res = self.stack.pop() print(res) return print('error') def check(self, x): if work == 'pop': self.pop() elif work == 'top': self.top() else: self.push(int(work.split()[-1])) n = input() tmp = stack() for i in range(int(n)): work = input() tmp.check(work)