Home > Back-end >  Han asked luo tower C language recursion
Han asked luo tower C language recursion

Time:03-30

project functional requirements
Ancient times have A brahman tower, tower with A, B, C three tower, A tower has 64 plates of various sizes that next big, small, all the 64 plates from A to C, can use A, B or C, but every time can only move A plate, and do not allow the market on small,

The arithmetic idea for the
As A first step, the above n - 1 dish from A to B, with the help of mobile C to B;
The second step, the NTH plate from A move to C;
The third step, n - 1 plate from B again, with the aid of A move to C,

project implementation
/* * * * * * * * * * * * * * * * ROM tower han * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include

Void Hanoi (int, char, char, char);//Hanoi function declaration
Int n=0;//record steps moving

Void main ()
{
int num;
Printf (" please enter the plate number: ");
The scanf (" % d ", & amp; Num);
Printf ("========================\ n ");
Hanoi (num, 'A', 'B', 'C');//Hanoi function called
Printf ("========================\ n ");
}

Void Hanoi (int num, char a, b char, char c)
{
If (num==1)
{//will be the first plate directly from a to b
Printf (" Step 3 d: % % c - & gt; % \ n "c + + n, a, c);
}
The else
{
Hanoi (num 1, a, c, b);//to n - 1 dish from a through c to b
Printf (" Step 3 d: % % c - & gt; % \ n "c + + n, a, c);//move the NTH plate from a to b to c
Hanoi (num 1, b, a, c);//to n - 1 plate borrowed from a to c b
}
}


operating effects



CodePudding user response:

CodePudding user response:

Take notes? Consider using impression or youdao note ~

CodePudding user response:

Ah, good,
  • Related