解决汉诺塔问题。[printf("(++c).move disk n from x to z\n",);即move(x,n,z)]
void hanoi(int n,int x,int y,int z)
{int c=0;
if(n==1)printf("%d".move disk %d from %d to %d\n",++c,n,x,z);
else
{hanoi(n-1,x,z,y);
printf("%d.move disk %d from %d to %d\n",++c,n,x,z);
hanoi(n-1,y,x,z);
}
}
main()
{int n,x,y,z;
scanf(%d %d %d %d",&n,&x,&y,&z);
hanoi(n,x,y,z);
}
评论