汉诺塔问题

OpenJudge-6261-汉诺塔问题open in new window

实现

#include <iostream>
using namespace std;

void hanoi (int n, char a, char b, char c) { // a -> b
    if (n == 1) {
        printf("%c->%d->%c\n", a, n, b);
        return;
    }
    hanoi(n - 1, a, c, b);
    printf("%c->%d->%c\n", a, n, b);
    hanoi(n - 1, c, b, a);
}

int main () {
    int n;
    char a, b, c;
    scanf("%d %c %c %c", &n, &a, &b, &c);
    hanoi(n, a, b, c);
    return 0;
}
最后修改于: