腹黑总裁只疼宝贝甜心:汉诺塔

来源:百度文库 编辑:九乡新闻网 时间:2024/05/04 14:30:27
#include
//移动步骤计数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;}