1. 题目
在终端输入汉诺塔层数n,实现将n层汉诺塔通过三座塔座A、B、C进行排列
2. 代码
#include <stdio.h>int hannuota(int len, int str, int tmp, int dst)
{if (1 == len){printf("%c -> %c\n", str, dst);}else{hannuota(len-1, str, dst, tmp);printf("%c -> %c\n", str, dst);hannuota(len-1, tmp, str, dst);}
}int main(void)
{char str = 'A';char tmp = 'B';char dst = 'C';int len = 0;scanf("%d", &len);hannuota(len, str, tmp, dst);return 0;
}
3. 程序结果