I used to learn things in the past, but now I have learned things from the C language. But I really don't understand things in class. I didn't understand what it was like until I was in my sophomore year, the programming I feel is really a window, but it takes time to understand and absorb it.
Description: There is A tower with three columns, A, B, and C. At first, there were n plates on column A, which were stacked from big to small in sequence and from bottom to top. They were required to be moved to column C. During the movement, column B could be used, however, only one tray can be moved at a time, and the three columns must always be kept under the tray, and the small disk must be in the upper state. The steps that require programmed output to move.
The Code is as follows:
Copy codeThe Code is as follows: # include <stdio. h>
Int move (char one, char two) // This function is used to display the output result intuitively. For example, if there is only one plate, output a --> c. in this way, each step of moving n plates is displayed.
{
Printf ("% c --> % c \ n", one, two );
}
Int hanoi (int n, char one, char two, char three) // is the core function.
{
Int move (char one, char two );
If (n = 1) // when there is only one plate, it is good to move directly from column A to column c, which is also an exit of recursive Programs.
Move (one, three );
Else {// more than a moment
Hanoi (n-1, one, three, two); // first move n-1 plates above from column A to Column B with the help of column c
Move (one, three); // at this time, only one plate is left in column A. move it to column c.
Hanoi (n-1, two, one, three); // move the n-1 plates left by Column B to column c with column A, and the task is completed.
}
}
Int main ()
{
Int n;
Char a, B, c;
While (scanf ("% d", & n )! = EOF) // you can enter multiple data validation results
Hanoi (n, 'A', 'B', 'C ');
}