#include<stdio.h>
/**
* Hanoi.
*/
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);
}// Of if
}// Of hanoi
/**
* Test the hanoi function.
*/
void hanoiTest(){
printf("--- addToTest begins. ---\r\n");
printf("2 plates\r\n");
hanoi(2, 'A', 'B', 'C');
printf("3 plates\r\n");
hanoi(3, 'A', 'B', 'C');
printf("--- addToTest ends. ---\r\n");
}// Of addToTest
/**
The entrance.
*/
int main(){
hanoiTest();
}// 0f main
运行结果如下
--- addToTest begins. ---
2 plates
A -> C
A -> B
C -> B
3 plates
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B
--- addToTest ends. ---
--------------------------------
Process exited after 0.02552 seconds with return value 0
请按任意键继续. . .