#include<stdio.h>int step=0;
int h[27]={0};
int move(charfrom,char to,int num){
step++;
printf("move %d disk from %c to %c\n",num,from,to);
return0;
}
int hanoi(int n,charfrom,charin,char to){
if(n==1){
move(from,to,1);
return0;
}
hanoi(n-1, from, to, in);
move(from,to,n);
hanoi(n-1,in,from,to);
return0;
}
int main()
{
int n;
printf("please input the number of the disks:\n");
scanf("%d",&n);
hanoi(n,'x','y','z');
printf("the step is %d",step);
return0;
}
查表
#include<stdio.h>
long a[60];
int main(){
int n;
int i;
a[0]=0;
a[1]=1;
for(i=2;i<60;i++){
a[i]=2*a[i-1]+1;
}
printf("please input the number of the discs\n");
scanf("%d",&n);
printf("you need %li steps to finish the hanoi problem",a[n]);
return0;
}