编程:递归编程解决汉诺塔问题(用java实现)

Answer:

//Li Cuiyun,October 14,2016.
//用递归方法编程解决汉诺塔问题
package tutorial_3_5;
import java.util.*; public class HanoiTower { public static void main(String[] args) {
// TODO Auto-generated method stub @SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Please enter the number of your dished(Hanoi Tower):");
n=sc.nextInt();
System.out.println("The number of the times you need to move the dishes is:"+new HanoiTower().hanoiTower(n)); } public int hanoiTower(int n)
{if(n==1) return 1;
else return hanoiTower(n-1)*2+1;
} }
上一篇:Python初学者应了解的技巧


下一篇:C++入门经典-例4.3-函数的递归调用之汉诺塔问题