- --------------------------------
- #include <stdio.h>
- #include <stdlib.h>
- static int count = 0;
- int print( char x,char y)
- {
- printf( "%c ---> %c/n", x, y);
- return ++count;
- }
- void move(int n,char x,char y,char z)
- {
- int count=0;
- if(n==1)
- {
- print(x,z);
- }
- else
- {
- move(n-1,y,x,z);
- print(x,z);
- //count++;
- move(n-1,z,y,x);
- // return count;
- }
- }
- void main(void)
- {
- int m;
- printf("输入汉诺塔层数:/n");
- scanf("%d",&m);
- move(m,'A','B','C');
- printf("移动了%d次/n",count);
- system("pause");
- }
c典型算法 递归
最新推荐文章于 2024-07-24 16:46:42 发布