【codeup墓地】1744: 算法3-5:n阶Hanoi塔问题(递归写法)

原题链接

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;

const int MAX = 10010;

int N, c = 0;
void Hanoi(int n, char x, char y, char z)
{
    if(n == 1)
    {
        printf("%2d. Move disk %d from %c to %c\n", ++c, n, x, z);
    }
    else
    {
        Hanoi(n-1, x, z, y);
        printf("%2d. Move disk %d from %c to %c\n", ++c, n, x, z);
        Hanoi(n-1, y, x, z);
    }
}

int main()
{
    while(scanf("%d", &N) != EOF)
    {
        c = 0;
        Hanoi(N, 'X', 'Y', 'Z');
        printf("\n");
    }
	return 0;
}



    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/m0_37454852/article/details/88559661
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞