Skip to content

Instantly share code, notes, and snippets.

@tamarous
Last active November 3, 2016 14:39
Show Gist options
  • Save tamarous/484705524f6a0a7ad4f957e02bf8e9dc to your computer and use it in GitHub Desktop.
Save tamarous/484705524f6a0a7ad4f957e02bf8e9dc to your computer and use it in GitHub Desktop.
汉诺塔问题
void hanoi(int n ,char source, char dest, char mid)
{
if (n == 1) {
// 如果 n = 1, 那么可以直接从source 挪到 dest
printf("%d from %c to %c\n",n,source,dest);
} else {
// 上面的n-1个,先放到作为缓冲的那个柱子上,好把最下面一个给移开
hanoi(n-1, source, mid, dest);
printf("%d from %c to %c\n",n-1,source,mid);
// 再将这n-1个,从作为缓冲的那个柱子上移到最终目的地上。
hanoi(n-1,mid,dest,source);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment