目录
5.递归
6-递归实例:汉诺塔问题
思路:
详细过程:
代码:
5.递归
调用自身
结束条件
6-递归实例:汉诺塔问题
思路:
结果:
详细过程:
代码:
#n为盘子的个数 a,b,c分别为3个地方. def hannuta(n,a,b,c): if n>0: hannuta(n-1,a,c,b) #将n-1个从a经过c移到到b(a-->b) print("moving %s from %s to %s" % (n,a,c)) #把第n个从a移动到c hannuta(n-1,b,a,c) #把n-1个从b经过a移动到c(b-->c) hannuta(3,'A','B','C') --------------------------------- moving 1 from A to C moving 2 from A to B moving 1 from C to B moving 3 from A to C moving 1 from B to A moving 2 from B to C moving 1 from A to C