题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
class IOS: def __init__(self): self.lst = [] def main(self, lst01, lst02, lst03): """ 迭代求方案: 过程必须切片不能操作表 :param lst01: 等待入栈 :param lst02: 栈 :param lst03: 出栈顺序记录 :return: lst出栈顺序汇总 """ if len(lst01) == 0 and len(lst02) == 0: self.lst.append(lst03) # 获取一种方案 if len(lst01) > 0: # 可以入栈 self.main(lst01[1:], lst02 + [lst01[0]], lst03[:]) if len(lst02) > 0: # 可以出栈 self.main(lst01[:], lst02[:-1], lst03 + [lst02[-1]]) def result(self): self.lst.sort() for i in self.lst: for j in i: print(str(j), end=" ") print() if __name__ == "__main__": n = int(input()) l = list(map(int, input().split())) ob = IOS() ob.main(l, [], []) ob.result()