C++实现递归的算法,具体代码如下:
# include <stdio.h>
void move(char A, int n, char C)
{
printf("%d: %c-->%c\n", n, A, C);
}
void Hanoi(int n, char A, char B, char C)
{
if (n == 1)
move(A, 1, C);
else
{
Hanoi(n-1, A, C, B);
move(A, n, C);
Hanoi(n-1, B, A, C);
}
}
int main(void)
{
int n;
char ch1 = 'A', ch2 = 'B', ch3 = 'C';
printf("请输入要移动的盘子的个数:");
scanf("%d", &n);
Hanoi(n, 'A', 'B', 'C');
return 0;
}
更多信息请查看IT技术专栏