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

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

时间:2016-10-25 02:23:54      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:汉诺塔   ext   out   scanner   main   res   warnings   问题   []   

//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实现)

标签:汉诺塔   ext   out   scanner   main   res   warnings   问题   []   

原文地址:http://www.cnblogs.com/6354-aa/p/5995219.html

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