【C语言】汉诺塔 移动盘子

avatar

azurekiln

// 汉诺塔 移动盘子
#include <stdio.h>

int step = 0;
int main() {
    void hanoi(int n, char one, char two, char three);
    int m;

    printf("请输入盘子数量:");
    scanf("%d", &m);
    hanoi(m, 'A', 'B', 'C');

    return 0;
}

void move(int n, char x, char y) {
    step++;
    printf("\nStep %d:Move disk%d from %c to %c", step, n, x, y);
}

void hanoi(int n, char one, char two, char three) {
    void move(int n, char x, char y);
    if (n==1) {
        move(n, one, three);
    } else {
        hanoi(n-1, one, three, two);
        move(n-1, one, three);
        hanoi(n-1, two, one, three);
    }

}

扫描二维码,在手机上阅读
收藏
请先 登录 再评论
powered by emlog pro
服务器供应商 景云数据

友情链接
白衣Ink


sitemap