问题变量:
柱子: A B C。
有3个从小到大的圆盘,依次的摆在A上面,要挪到中间的柱子B上,借助柱子C。
#include<stdio.h>
void han(int N,char from1,char to1,char help1){
if(N==1){
printf("move %d from %c to %c",N,from1,to1);
return ;
}
han(N-1,from1,help1,to1);
{
printf("move %d from %c to %c\n",N,from1,to1);
}
han(N-1,help1,to1,from1);
}
int main(){
int h=3;
char A=A;
char B=B;
char C=C;
han(h,'A','B','C');
return 0;
}
结果输出:
move 1 from A to Bmove 2 from A to C
move 1 from B to Cmove 3 from A to B
move 1 from C to Amove 2 from C to B
move 1 from A to B