Data structure algorithm C language implementation (10)---3.3 Stack and recursive implementation

Source: Internet
Author: User

I. INTRODUCTION

Hanoi Tower Problem is a typical example of recursion, and the explanation in the book is very detailed, it is helpful to understand the working mechanism of C language function and function transfer, it is worth seeing. Moreover, recursion in my opinion and division, DP, greedy and so on is a very beautiful thought, it is worth learning!!!

Two. cpp file

1 //3_3.cpp2 /**3 Author:zhaoyu4 Email:[email protected]5 date:2016-6-86 note:realize My textbook << data structure (C-language version) >>7 */8 //Page9#include <cstdio>Ten intCNT =0; One voidMoveCharAintNCharb) A { -printf"%i\tmove disk%d from%c to%c\n", ++CNT, N, a, b); - } the voidHanoiintNCharXCharYCharz) - { -     if(1==N) -     { +Move (x,1, z); -     } +     Else A     { atHanoi (n1, X, z, y); - Move (x, n, z); -Hanoi (n1, y, x, z); -     } - } - intMainintargcChar Const*argv[]) in { -     intN; toscanf"%d", &n); +Hanoi (N,'X','Y','Z'); -     return 0; the}
3_3.cpp

Three. Testing

Three:

  

Five:

  

Easy to see, moving N, need 2^n-1 times.

  

Data structure algorithm C language implementation (10)---3.3 Stack and recursive implementation

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.