#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void move(char x, char y)//将一个盘子从x移动到y
{
printf("%c→%c\n", 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);
}
}
int main()
{
int m;
printf("input thr number of dislike:");//提示输入汉诺塔的阶数
scanf("%d", &m);
printf("The step to move %d dislikes:", m);//打印步骤
hanoi(m, 'A', 'B', 'C');
}
汉诺塔问题
最新推荐文章于 2024-08-14 14:09:29 发布