用Visual C++怎么做Hanoi
来源:百度知道 编辑:UC知道 时间:2024/05/14 15:23:04
#include "stdio.h"
void move(char x,char y)
{ static int i=0;
printf("%3d.%c-->%c\n",++i,x,y);
}
void hanoi(int n,char one,char two,char three)
{ /* 将n个盘子从one座借助two座,移到three座 */
if(n==1) move(one,three);
else
{ hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void main()
{
int m;
printf("输入盘子数:");
scanf("%d",&m);
printf("移动%d个盘子的步骤为:\n",m);
hanoi(m,'A','B','C');
}