1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| # include <stdio.h>
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); } }
int main(void) { int n; printf("请输入要移动盘子的个数: "); scanf("%d", &n); hannuota(n, 'A', 'B', 'C'); return 0; }
|