返回顶部

收藏

自己编的汉诺塔游戏过程

更多
def tower(a,b,c,n):
    if n==1:
        print "put 1 from b to a."
    elif n==2:
        print "put 1 from %r to %r." % (b,c)
        print "put 2 from %r to %r." % (b,a)
        print "put 1 from %r to %r." % (c,a)
    else:
        tower(c,b,a,n-1)
        print "put %d from %r to %r." % (n,b,a)
        tower(a,c,b,n-1)

def MITtower(a,b,c,n):
    if n==1:
        print "put", n, "from", b, "to", a
    else:
        MITtower(c,b,a,n-1)
        print "put",n,"from" ,b, "to" ,a 
        MITtower(a,c,b,n-1)

标签:python

收藏

0人收藏

支持

0

反对

0

发表评论