代码如下
#include <stdio.h>
void hanoi(int paraN, char paraSource, char paraDestination, char paraTransit)
{
if (paraN <= 0) {
return;
}
else {
hanoi(paraN - 1, paraSource, paraTransit, paraDestination);
printf("%c - > %c \r\n", paraSource, paraDestination);
hanoi(paraN - 1, paraTransit, paraDestination, paraSource);
}
}
void hanoiTest()
{
printf("---- hanoiTest begins. ----\r\n");
printf("2 plates\r\n");
hanoi(2, 'A', 'B', 'C');
printf("3 plates\r\n");
hanoi(3,'A','B','C');
printf("4 plates\r\n");
hanoi(4, 'A', 'B', 'C');
printf("---- hanoiTest ends. ----\r\n");
}
int main()
{
hanoiTest();
return 0;
}
运行结果
汉诺塔问题需要用计算机思维理解较为简单