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

Posted 耶路撒冷有多冷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程:递归编程解决汉诺塔问题(用java实现)相关的知识,希望对你有一定的参考价值。

//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;
}

}


















以上是关于编程:递归编程解决汉诺塔问题(用java实现)的主要内容,如果未能解决你的问题,请参考以下文章

Java编程用栈来求解汉诺塔问题的代码实例(非递归)_java - JAVA

课后作业2:递归编程解决汉诺塔问题

分治算法——汉诺塔问题

汉诺塔的C语言代码怎么写啊

用类比方式学习编程中函数递归(个人理解仅供参考)(内含汉诺塔问题的求解)

递归解决汉诺塔问题