- #include "stdio.h"
- void hanoi(int n,char one ,char two,char three);
- void move(char aa,char bb);
- int main()
- {
- int n;
- printf("汉诺塔问题,将盘子从a,借助于b移动到c.\n");
- printf("请输入盘子数:");
- scanf("%d",&n);
- printf("移动顺序为:\n");
- hanoi(n,'a','b','c');
- return 0;
- }
- //将n个盘子从one借助于two移动到three
- void hanoi(int n,char one ,char two,char three)
- {
- if(n==1)
- {
- move(one,three);
- }
- else
- {
- hanoi(n-1,one,three,two);
- move(one,three);
- hanoi(n-1,two,one,three);
- }
- }
- //从a移动到b
- void move(char a,char b)
- {
- printf("%c--->%c\n",a,b);
- }
来源: http://www.phpxs.com/code/1004241/