码迷,mamicode.com
首页 > 编程语言 > 详细

python 递归-汉诺塔

时间:2019-08-06 00:54:57      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:color   code   int   python 递归   format   pytho   one   div   style   

# 汉诺塔
a = "A"
b = "B"
c = "C"
def hano(a, b, c, n):
    if n == 1:
        print("{} --> {}".format(a, c))
    if n == 2:
        print("{} --> {}".format(a, c))
        print("{} --> {}".format(a, b))
        print("{} --> {}".format(b, c))
        return None
    hano(a, b, c, n - 1)
    print("{} --> {}".format(a, c))
    hano(b, a, c, n - 1)

hano(a, b, c, 5)

 

 

A --> C
A --> B
B --> C
A --> C
B --> C
B --> A
A --> C
A --> C
B --> C
B --> A
A --> C
B --> C
A --> C
A --> B
B --> C
A --> C
B --> C
B --> A
A --> C
B --> C
A --> C
A --> B
B --> C
B --> C
A --> C
A --> B
B --> C
A --> C
B --> C
B --> A
A --> C

 

python 递归-汉诺塔

标签:color   code   int   python 递归   format   pytho   one   div   style   

原文地址:https://www.cnblogs.com/vvrr/p/11306339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!