/********************************
递归函数——————汉诺塔问题
********************************/
#include <stdio.h>
void Move(int n,char a,char b);
void Hanoi(int n,char a,char b,char c)
{
if(n==1)
{
Move(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); //借用A从C移到B
}
}
void Move(int n,char a,char b)
{
printf("Move %d:from %c to %c\n",n,a,b);
}
int main()
{
int n;
printf("Input the number of disks:");
scanf("%d",&n);
printf("moving %d disks from a to b:\n",n);
Hanoi(n,'A','B','C');
return 0;
}
递归函数——————汉诺塔问题
********************************/
#include <stdio.h>
void Move(int n,char a,char b);
void Hanoi(int n,char a,char b,char c)
{
if(n==1)
{
Move(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); //借用A从C移到B
}
}
void Move(int n,char a,char b)
{
printf("Move %d:from %c to %c\n",n,a,b);
}
int main()
{
int n;
printf("Input the number of disks:");
scanf("%d",&n);
printf("moving %d disks from a to b:\n",n);
Hanoi(n,'A','B','C');
return 0;
}