递归基础—汉诺塔
import java.util.Scanner;
public class hannuota {
public static int step = 1;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
hannuota(i,'a','b','c');
}
public static void hannuota(int n , char a ,char b ,char c){
if(n == 1)
System.out.println("第"+step++ +"步:将"+n+"从"+a+"移动到"+c); //递归一定要声明出口,此处为出口
else {
hannuota(n - 1, a, c, b); //把n-1 个方块从a,通过c,移到b
System.out.println("第"+step++ +"步:将"+n+"从"+a+"移动到"+c);
hannuota(n - 1, b, a, c); //把n-1 个方块从b,通过a,移到c
}
}
}