美文网首页
【基础】练习册39-Python3_河内塔

【基础】练习册39-Python3_河内塔

作者: Alyna_C | 来源:发表于2021-02-04 06:55 被阅读0次

河内塔

代码如下:

#河内塔

def moveTower(height,fromPole,toPole,withPole):

    if height>=1:

        moveTower(height-1,fromPole,withPole,toPole)

        moveDisk(fromPole,toPole)

        moveTower(height-1,withPole,toPole,fromPole)

def moveDisk(fp,tp):

    print("moving disk from",fp,"to",tp)

moveTower(4,"A","B","C")


运行结果为:

moving disk from A to C

moving disk from A to B

moving disk from C to B

moving disk from A to C

moving disk from B to A

moving disk from B to C

moving disk from A to C

moving disk from A to B

moving disk from C to B

moving disk from C to A

moving disk from B to A

moving disk from C to B

moving disk from A to C

moving disk from A to B

moving disk from C to B

相关文章

网友评论

      本文标题:【基础】练习册39-Python3_河内塔

      本文链接:https://www.haomeiwen.com/subject/eqwstltx.html