汉诺塔c++

汉诺塔移动轨迹程序c++

思想

  • 递归
  • 辅助空间

代码区

#include<iostream>

using namespace std;

bool Hano(int n, char A, char B, char C)
{
    if (n == 1)
    {
        cout <<  "移动圆圈数" << n << "从盘" << A << "盘" << B << endl;
        return true;
    }
    else
    {
        Hano(n - 1, A, C, B);
        cout << "移动圆圈数" << n << "从盘" << A << "盘" << C << endl;
        Hano(n - 1, C, B, A);
    }
}

int main()
{
    int n;
    while (cin >> n)
    {
        bool Judge = Hano(n, 'l', 'r', 'm');
        if (Judge)
            cout << "移动完成" << endl;
    }
    return 0;
}
    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/weixin_40291215/article/details/82559763
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞