Simple solution Code _c language for the Nottingham tower problem

Source: Internet
Author: User

I used to have a solid knowledge of things. Now pick it up, Hanoi this is the C language to learn things, but the class was really don't understand, until sophomore, just understand that is the way, I feel the programming, really is a window paper, but Pierce to take time to understand absorption.

Topic Description: There is a tower, the tower has a,b,c three pillars. At first, there were n plates on column A, in turn, from large to small, stacked from bottom to top, require all of them to move to the C pillar, in the process can be used to move the B column, but only to move to a plate, and must be three pillars always keep the market at the bottom, small disk in the state. The steps required to move the programming output.

The code is as follows:

Copy Code code as follows:

#include <stdio.h>
int move (char One,char two)//This function is visually displayed for output results, such as when there is only one plate, the output is a-->c. So, move n a plate and show it every step.
{
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 only one plate is moved directly from column A to pillar C, it is also an exit of the recursive program
Move (One,three);
else{//plate is more than a momentary
Hanoi (N-1,one,three,two)//First use C column to move the upper n-1 plate from column A to column B
Move (One,three); At this point a column only a plate, move to c column can
Hanoi (N-1,two,one,three)//The remaining n-1 plate of column B is moved to column C with the help of a column, and the task is completed.
}
}
int main ()
{
int n;
Char a,b,c;
while (scanf ("%d", &n)!=eof)//You can enter multiple sets of data test results
Hanoi (N, ' a ', ' B ', ' C ');
}

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.