Simple Solution code

Source: Internet
Author: User

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 ');
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.