首页 > 代码库 > 栈和递归之Hanoi塔

栈和递归之Hanoi塔

hanoi塔   代码
<pre name="code" class="cpp">#include<stdio.h>
void move(char x,int n,char y)
{
	static int k=1;
	printf("Step %d : %d from %c >>->> to %c \n",k++,n,x,y);
}

void hanoi(int n,char A,char B,char C)
{
	if(n==1)
	{
		move(A, 1, C);
	}
	else
	{
		hanoi(n-1, A, C, B);
		move(A, n, C);
		hanoi(n-1, B, A, C);
	}
}

int main()
{
	int n;
	printf("请输入n的值:\n");
	while(~scanf("%d",&n))
	{ 
	    hanoi(n,'A','B','C');
	} 
	return 0;
} 




栈和递归之Hanoi塔