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; } }
浙公网安备 33010602011771号