#include <stdio.h> //移動(dòng)步驟計(jì)數(shù) static int number = 0; /* * 借助于x,從from移動(dòng)n個(gè)到to * * n 要移動(dòng)的個(gè)數(shù) * from 初始的位置 * to 移動(dòng)之后的文件 * 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個(gè)移動(dòng)到x暫存 hanluota(n-1, from, x, to); //把from最下面的一個(gè)移動(dòng)到to printf("%d : %c --> %c\n", number++, from, to); //把x上的n-1個(gè)移動(dòng)到to hanluota(n-1, x, to, from); } int main(void) { hanluota(3, 'a', 'b', 'c'); printf("finished"); getchar(); return 0; } |
|