Recursion - Tower of Hanoi

code show as below:

#include<stdio.h>
void Hannuota(int n,char A,char B,char C)
{
// if it is 1 plate
// Directly move the plate on master A to B with C
// else
// First move the n-1 plate on column A to B with the help of C
// directly move the plate on column A from A to C
// Finally, move the n-1 plates on the B column to C with the help of A
	if(n==1)
		printf("Move the plate numbered %d directly from the %c column to the %c column\n",n,A,C);
	else
	{
		Hannuota(n-1,A,C,B);
		printf("Move the plate numbered %d directly from the %c column to the %c column\n",n,A,C);
		Hannuota(n-1,B,A,C);
	}
}
intmain()
{
	char ch1='A';
	char ch2='B';
	char ch3='C';
	int n;
	printf("Output the number of plates\n");
	scanf("%d",&n);
	Hannuota(n,'A','B','c');
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325731757&siteId=291194637