可能重复:
这是如何工作的?奇怪的河内塔解决方案
在浏览 Google 时,我发现了河内塔的这个有趣的解决方案,它甚至不使用堆栈作为数据结构。
谁能简要解释一下,它实际上在做什么?
这种解决方案真的可以接受吗?
代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, x;
printf("How many disks?\n");
scanf("%d", &n);
printf("\n");
for (x=1; x < (1 << n); x++)
printf("move from tower %i to tower %i.\n",
(x&x-1)%3, ((x|x-1)+1)%3);
return 0;
}
更新:硬编码的数字 3 在这里做什么?