今天离散课上出现了一道汉诺塔问题,貌似大一做过,又试着做了一遍….
不过如果输入数字大点.貌似求解速度有点慢.我输入了16.用了整整40S,如果输入像老师说的64,不知道要多少时间,或者电脑直接死机了………….
C语言代码如下:
void hanoi(int n,char a,char b,char c)
{
if(n>0)
{
hanoi(n-1,a,c,b);
printf("\n move disc %d from pile %c to %c",n,a,b);
hanoi(n-1,c,b,a);
}
}
{
if(n>0)
{
hanoi(n-1,a,c,b);
printf("\n move disc %d from pile %c to %c",n,a,b);
hanoi(n-1,c,b,a);
}
}
int main()
{
int n;
printf("lease enter the number of disks to be moved:");
scanf("%d",&n);
hanoi(n,'a','b','c');
system("pause");
rerurn 0;
}