#include <stdio.h>

void moveHanoi(int n, int a, int c)
{
    printf("将 %d 号由 %c --> %c\n",n, a, c);
}

void Hanoi(int n, char a, char b, char c)
{
    if (n == 1)
    {
        moveHanoi(n, a, c);
    }
    else
    {
        Hanoi(n - 1, a, c, b);
        moveHanoi(n, a, c);
        Hanoi(n - 1, b, a, c);
    }
}

int main()
{
    int n = 3;
    char a = 'A', b = 'B', c = 'C';
    Hanoi(n, a, b, c);
    return 0;
}

一句话,将n -1 看作一个整体

共有 0 条评论