The third week of C + + practice of the __c++ tower problem

Source: Internet
Author: User
 
 /* *copyright (c) 2016, Yantai University Computer College 
 *all rights reserved. 
 * File name: Test2.cpp 
 *:    Liu Jinshi 
 * Completion Date: March 19, 2016 
 * version  number: v1.0 
 * Problem Description: Hanoi Tower Problem
 * Enter Description: Enter the number of plates you want to move. 
 * Output Description: Output move step. 
* *  
#include <iostream>
using namespace std;
void Hanoi (int n,char Src,char medium,char dest);
void Move (char Src,char dest);
int main ()
{

    int m;
    cout<< "Enter The number of Diskes:" <<endl;
    cin>>m;
    cout<< "The Step to Moving" <<m<< "diskes:" <<endl;
    Hanoi (M, ' A ', ' B ', ' C ');

    return 0;
}
void Hanoi (int n,char Src,char Medium,char dest)
{
    if (n==1) move
        (src,dest);
    else
    {
        Hanoi (N-1,src,dest,medium);//n-1 plate from SRC to Medium, with the help of Dest, move
        (src,dest);
        Hanoi (N-1,medium,src,dest), and/or n-1 tray from medium to dest, with the help of Src
    }
}
void Move (char Src,char dest)
{
    cout<<src<< "-->" <<dest<<endl;
Run Result:
Programming Experience:
<span style= "White-space:pre" >	</span> through this Hanoi tower problem, so that their deeper understanding of the function recursion, as well as the use of recursion to solve complex problems. Take the subject, to move n plates to the dest, first of all to the top n-1 plate moved to the medium, and then the last plate from SRC directly moved to dest up. But the n-1 plate from SRC moved to the medium obviously can not be completed in one step, to the first n-2 plate moved to dest, and then the SRC on the n-1 plate to move to the medium ... And so on. Finally, move the largest plate to the dest and then move the n-1 plate to the dest, and so on.
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.