题解 | #火车进站#学习大佬的递归

火车进站

http://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

def fun(in_list, stack, out_list):

if len(in_list) == 0 and len(stack) == 0:
    res.append(out_list)
# 有等待进站的火车时
if len(in_list) > 0:
    fun(in_list[1:], stack + [in_list[0]], out_list)
# 有等待出站的列车,先出最后一列车
if len(stack) > 0:
    fun(in_list, stack[:-1], out_list + [stack[-1]])
return res

while True:

try:
    n = int(input())
    order_list = list(map(int, input().split()))
    res = []
    
    result = fun(order_list, [], [])
    for i in sorted(result):
        print(' '.join(map(str, i)))

except:
    break
全部评论

相关推荐

宇算唯航:目测实缴资本不超100W的小公司
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
2
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务