Implement the tower program in C Language

Source: Internet
Author: User

/*
This program is to simulate the operation process of the tower,
Print the execution method of each step,
And record the number of executions;

The main program uses recursion;
Wait for 0.5 seconds for each execution step;
*/
# Include <stdio. h>
# Include <conio. h>/* for the function: clrscr ()*/
# Include <dos. h>/* for the function: delay (int ms )*/
Int COUNT = 0;/* Move times */

/* Move the disk from column A to Column B */
Void move (char a, char B)
{
Delay (500);/* Wait 0.5 s */
Printf ("/n % d: % C-> % C", ++ count, a, B );
}

/* Tower Master process */
Void Hanoi (int n, char a, char B, char C)
{
If (n <1)
Return;
If (n = 1)
{
Move (a, c );
Return;
}
Hanoi (n-1, A, C, B );
Move (a, c );
Hanoi (n-1, B, A, C );
}

Int main ()
{
Int n = 0;/* Number of plates */
Clrscr ();/* clear the screen */

Printf ("Please input a INT :");
Scanf ("% d", & N );

/* Main process */
Hanoi (n, 'A', 'B', 'C ');

/* Exit with any key */
Printf ("/npress any key to exit! ");
Return getch ();/* wait to get a char */
}

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.