递归例子之汉诺塔

#include
//只要将第一轮想明白就好了,千万别深究
//递归出口:只有一个disk的时候
//递归式子:n个盘子搬来搬去
void hanio(int n, char a, char b, char c);
int main(int argc, const int * argv)
{
int n;
printf(“input the number of disk:”);
scanf("%d", &n);

printf("the steps for %d disk are: \n", n);

hanio(n, 'a','b','c');

return 0;

}

void hanio(int n, char a, char b, char c)
{
if(1 == n)
{
printf("%c–>%c\n", a, b);
}
else
{
hanio(n-1, a, c, b);
printf("%c–>%c\n", a, b);
hanio(n-1, c, b, a);
}

}

你可能感兴趣的:(C语言基础,递归)