#include <stdio.h> //移动步骤计数 static int number = 0; /* * 借助于x,从from移动n个到to * * n 要移动的个数 * from 初始的位置 * to 移动之后的文件 * x 需要借助的位置 * */ void hanluota(int n, char from, char to, char x) { if(n <= 1) { printf("%d : %c --> %c\n", number++, from, to); return; } //把from上的n-1个移动到x暂存 hanluota(n-1, from, x, to); //把from最下面的一个移动到to printf("%d : %c --> %c\n", number++, from, to); //把x上的n-1个移动到to hanluota(n-1, x, to, from); } int main(void) { hanluota(3, 'a', 'b', 'c'); printf("finished"); getchar(); return 0; } |
|