#include<stdio.h>
int count=0;
void Move()//声明move
{
count++; //统计步数
}
void hanoi(int n,char a,char b,char c)//声明hanoi
{
if(n==1)
Move(a,c);
else
{
hanoi(n-1,a,c,b);
Move(a,c);
hanoi(n-1,b,a,c);
}
}
int main()
{
int m,g=0;
while(scanf("%d\n",&m)!=EOF)
{
hanoi(m,'a','b','c');
printf("%d\n",count);
}
return 0;
}
你的Move函数是无参的函数,调用的时候怎么有参数了?
Move函数核心代码你隐藏了吧,没啥可优化的,代码已经比较简洁了。只要结果OK就行
优化是吧?首先,试试看能不能把递归改成循环。