Arithmetic

B站-6.递归之汉诺塔

PineappleCat · 3月10日 · 2020年 421次已读
```
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
/*
递归:汉诺塔
*/
int main()
{
    char ch1 = 'A';
    char ch2 = 'B';
    char ch3 = 'C';
    int n;
    printf("请输入要移动盘子的个数:");
    scanf("%d",&n);
    hannuota(n,'A','B','C');
    return 0;
}

void hannuota(int n,char A,char B,char C){
    if(1==n){
        printf("将编号为%d的盘子直接从%c柱子移动到%c柱子\n",n,A,C);
    }else{
        hannuota(n-1,A,C,B);
        printf("将编号为%d的盘子直接从%c柱子移动到%c柱子\n",n,A,C);
        hannuota(n-1,B,A,C);
    }
}
```


Click here to view the copyright notice of this site(点击此处查看本站版权声明)


0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!