汉诺塔
import java.util.Scanner; public class 汉诺塔 { static int num=0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); move(n); System.out.println(num); } public static int move(int dish) { num++; if (dish == 1) {//圆盘只有一个的时候 将其从a移动到c return num; } else { move(dish-1);//a为初始塔座,b为目标塔座,c为中介塔座 move(dish-1);//b为初始塔座,c为目标塔座,a为中介塔座 return num; } } }

浙公网安备 33010602011771号