C language implementation of the tower of Hanoi Problem

Source: Internet
Author: User
Lingta-Solution

1. If there is only one gold tablet, move it from the source to the target rod and end it.

2. If there are n gold slices, move n-1-1 gold slices to the secondary rods, move themselves to the target rods, and then move the first n-1 gold slices to the target rods.

 //  Tower of Hanoi Tower  
# Include < Stdio. h>
Void Toh ( Int N, Char A, Char B, Char C)
{
If (N = 1 )
{
Printf ( " Movedisk % d from % C to % C \ n " , N, a, c );
Return ;
}
Toh (n - 1 , A, C, B );
Printf ( " Movedisk % d from % C to % C \ n " , N, a, c );
Toh (n - 1 , B, A, C );
}

Void Main ()
{
Int M, N;
Printf ( " Enter the number of times of test data " );
Scanf ( " % D " , & M );
While (M ! = 0 )
{
Printf ( " Enter the number n to solve the n-level tower issue: \ n " );
Scanf ( " % D " , & N );
Toh (n, ' A ' , ' B ' , ' C ' );
M -- ;
If (M = 0 )
{
Return ;
}
}
}
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.