The second dish of chicken nanoscale white tower blog --Hanoi recursive function

Tower of Hanoi is very interesting, but I think in order to understand the function most of the day, this is my ability to understand the problem, and I hope to bring big brother can take me, understanding is not very good welcome students learn together, oh.

/*汉诺塔其实就是从一个柱子上的几个小盘子
通过一个中介的柱子转到目的地的柱子*/
#include <iostream>
using namespace std;
void move(int n,char A,char B,char C)
{
   if(n==1)
   cout<<A<<"->"<<C<<endl;

else{move(n-1,A,C,B);/*运用递归函数,其实就是move3运用的是move2就是为了将除了最大盘之外的盘子全部由 A->B,move2也就运用move1*/
cout<<A<<"->"<<C<<endl;//最大的盘子就可以搬到目的地C了
move(n-1,B,A,C);//然后就是其他的小盘子从B搬到C也到达了目的地
        }
}
int main()
{
    int n;
    cin>>n;
    move(n,'A','B','C');
    return 0;

}

Released three original articles · won praise 0 · Views 53

Guess you like

Origin blog.csdn.net/qq_45871768/article/details/103481991