程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Solving Hanoi Tower problem with Python

編輯:Python

Hanoi Tower problem description

There are three thimbles , On the first thimble were several plates , And the small plate is on the big plate , Now move these plates from the first thimble to the third thimble , And the small plate is still on the big plate . The second thimble can be used during the movement , Only one plate can be moved at a time , And the small plate should be on top of the big plate , How to move all the plates from the first thimble to the third thimble .

Solve the mind :

Adopt the method of recursive iteration , First, I will n-1 Move the first plate to the second thimble , Move the plate on the first thimble to the ` Three thimbles , In the same way n-1 One plate moves to the third plate .

Hanoi Tower problem programming

def hanoni(n,x,y,z):
if(n==1):
print(x,'--->',z);
else:
hanoni(n-1,x,z,y);
print(x,'--->',z);
hanoni(n-1,y,x,z);
n = int(input(' Please enter the number of floors of Hanoi tower :'));
hanoni(n,'x','y','z');

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved