您的位置:首页 > 其它

汉诺塔

2015-11-07 23:55 316 查看
#include <stdio.h>
void Move(int n,char a,char b)
{
printf("Move %d : from %c to %c\n",n,a,b);
}
//函数功能:用递方法将n个圆盘借助于柱子c从源柱子a移动到目标柱子b上
void Hanoi(int n,char a,char b,char c)
{
if(n == 1)
{
Move(n,a,b);//将第n个圆盘由a移动到b
}
else{
Hanoi(n-1,a,c,b);//将第n-1个圆盘借助于b由a移动到c
Move(n,a,b);//将第n个圆盘由a移动到b
Hanoi(n-1,c,b,a);//将第n-1个圆盘借助于a由c移动到b
}
}
int main()
{
int n;
printf("Input the number of disks:\n");
scanf("%d",&n);
printf("Steps of moving %d disks from A to B by means of C:\n", n);
Hanoi(n,'A','B','C');//将n个圆盘借助C由A转移到B
}


其实我没有搞懂,直接从书上搬来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: