这个递归函数运行好慢,我输入50,几乎用了近半个小时还没有完,不知有无其他能提高效率的方法吗
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
void hanoi();
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to moving %3d diskes:\n",m);
hanoi(m,'A','B','C');
return 0;
}
void move (getone,putone)
char getone,putone;
{
printf("%c---->%c\n",getone,putone);
}
void hanoi(n,one,two,three)
char one ,two,three;
int n;
{
if (n==1)move(one,three);
else{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
|