提示:该blog仅为了完成作业,大佬请绕路
老师的代码
#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.
*/
void main() {
hanoiTest();
}// Of main
我的代码
#include <stdio.h>
#include <string.h>
/*
算法思路:1将 n-1个盘子先放到B座位上
2.将A座上地剩下的一个盘移动到C盘上
3、将n-1个盘从B座移动到C座上
*/
//函数声明
void move(char x,char y)
{
printf("%c--->%c",x,y)
}
void hannuo(int n,char one ,char two,char three)
{
if(n==1)move(one, three); //递归截止条件
else
{
hannuo(n-1,one ,three,two);//将 n-1个盘子先放到B座位上
move(one,three);//将A座上地剩下的一个盘移动到C盘上
hannuo(n-1,two,one,three);//将n-1个盘从B座移动到C座上
}
}
int main()
{
int n;
printf("input your number");
scanf("%d",&n);
hannuo(n,'A','B','C');
return 0;
}