"Hanoi" Hanoi number of moves and move steps

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>//Number of movesintHanoiintnum) {    Static intall =0; if(num = =1)    {        return 1; }    Else{ All= Hanoi (Num-1) *2+1; }    returnAll ;}//Move StepsvoidHANOI2 (intNumCharACharBCharC) {    if(num = =1) {printf ("%c->%c\n", A,c); return 1; }    Else{hanoi2 (num-1, a,c,b); printf ("%c->%c\n", A, C); HANOI2 (Num-1, B,a, C); }}voidMain () {printf ("Please enter the number of Hanoi layers! \ n"); intnum; scanf_s ("%d", &num); printf ("\ n Number of moves =%d\n", Hanoi (num)); printf ("move the method as follows \ n"); HANOI2 (num,'A','B','C'); System ("Pause");}

The pursuit of running speed is best not recursive, the pursuit of simple code of the word with recursion.

Loops can be realized, recursion can also be realized, and recursive can be realized, the loop may not be realized.

Note that execution is performed in order before recursion, and the order of execution after recursion is reversed.

"Hanoi" Hanoi number of moves and move steps

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.