user-0 提问: 求解C语言递归算法代码

回复:
user: user-1
content: 汉诺塔只有几行

user: user-2
content: hi wang  /* 最后把y上面的n-2个盘移动到z上 */ move(n-2, y, z, x1, x2); 这个注释写错了，应该是/* 最后把y上面的n-2个盘移动到x2上  move(n-2, y, z, x1, x2);*/

user: user-2
content: hi wang  四个汉诺塔移动处理如下所示： 使用递归的方式 /* 借助y,z 把x1上面的n个盘移动到x2 */ void move(int n, hanstack *x1, hanstack *y, hanstack *z, hanstack *x2) { 	Elemtype tmp=0;  	if(n == 0){ 		// do nothing 	}else if(n == 1){ 		pop_stack(x1,&tmp); 		push_stack(x2,tmp); 		print_struct(); 	}else{ 		/* 先进行把x1上面的n-2个盘移动到y上 */ 		move(n-2, x1, z, x2, y); 		/* 把x1上面的到时第二个盘(第n-1个)移动到z上*/ 		pop_stack(x1,&tmp); 		push_stack(z,tmp); 		print_struct(); 		/* 把x1上面的最后一个盘(第n个)移动到x2上 */ 		pop_stack(x1,&tmp); 		push_stack(x2,tmp); 		print_struct(); 		/* 把z上面的盘(x1上的第n-1个)移动到x2上*/ 		pop_stack(z,&tmp); 		push_stack(x2,tmp); 		print_struct(); 		/* 最后把y上面的n-2个盘移动到z上 */ 		move(n-2, y, z, x1, x2); 	} }    四个汉诺塔移动模拟全代码(使用栈和递归)： #include <stdio.h> #include <stdlib.h>  #define MAX_NUM 10 #define MAX_RENUM 5  typedef int Elemtype;  typedef struct{ 	Elemtype *top; 	Elemtype *base; 	int stacksize; }hanstack;  hanstack a; hanstack b; hanstack c; hanstack d;  void init_stack(hanstack *s) { 	if(NULL == s){ 		exit(0); 	}  	s->base = (Elemtype *)malloc(MAX_NUM*sizeof(Elemtype)); 	if(NULL == s->base){ 		exit(0); 	}  	s->top=s->base; 	s->stacksize=MAX_NUM; }   void push_stack(hanstack *s, Elemtype in) { 	if((s == NULL) && (s->base) && (s->top)){ 		exit(0); 	}  	if((s->top - s->base) >= s->stacksize){ 		s->base = (Elemtype *)realloc(s->base,((s->stacksize+MAX_RENUM)*sizeof(Elemtype))); 		if(NULL == s->base){ 			exit(0); 		}  		s->top = s->base + s->stacksize; 		s->stacksize = s->stacksize + MAX_RENUM; 	}  	*(s->top) = in; 	s->top++;  	return; }  void pop_stack(hanstack *s, Elemtype *out) { 	if((s == NULL) && (s->base) && (s->top)){ 		exit(0); 	}  	s->top--; 	*out = *(s->top);  	return; }  void clear_stack(hanstack *s) { 	if((s == NULL) && (s->base) && (s->top)){ 		exit(0); 	}  	free(s->base); 	s->base=NULL; 	s->top=NULL; 	s->stacksize=0;  }  void print_struct() { 	system("clear");  	int i=0;  	// 判断x.top!=NULL &&  x.base != NULL省略。  	for(i=a.stacksize; i>0; i--){ 		if(a.top-a.base >= i){ 			printf("     %2d", *(a.base+i-1)); 		}else{ 			printf("       "); 		}  		if(b.top-b.base >= i){ 			printf("     %2d", *(b.base+i-1)); 		}else{ 			printf("       "); 		}  		if(c.top-c.base >= i){ 			printf("     %2d", *(c.base+i-1)); 		}else{ 			printf("       "); 		}  		if(d.top-d.base >= i){ 			printf("     %2d", *(d.base+i-1)); 		}else{ 			printf("       "); 		}  		printf("\n"); 	}  	fflush(stdout);  //	sleep(1); //	usleep(200000);  // 0.2s 	usleep(500000);  // 0.2s }  /* 借助y,z 把x1上面的n个盘移动到x2 */ void move(int n, hanstack *x1, hanstack *y, hanstack *z, hanstack *x2) { 	Elemtype tmp=0;  	if(n == 0){ 		// do nothing 	}else if(n == 1){ 		pop_stack(x1,&tmp); 		push_stack(x2,tmp); 		print_struct(); 	}else{ 		/* 先进行把x1上面的n-2个盘移动到y上 */ 		move(n-2, x1, z, x2, y); 		/* 把x1上面的到时第二个盘(第n-1个)移动到z上*/ 		pop_stack(x1,&tmp); 		push_stack(z,tmp); 		print_struct(); 		/* 把x1上面的最后一个盘(第n个)移动到x2上 */ 		pop_stack(x1,&tmp); 		push_stack(x2,tmp); 		print_struct(); 		/* 把z上面的盘(x1上的第n-1个)移动到x2上*/ 		pop_stack(z,&tmp); 		push_stack(x2,tmp); 		print_struct(); 		/* 最后把y上面的n-2个盘移动到z上 */ 		move(n-2, y, z, x1, x2); 	} }   int main() { 	int i=0,n=0; 	Elemtype data=0;  	init_stack(&a); 	init_stack(&b); 	init_stack(&c); 	init_stack(&d);  	printf("please enter the num n:"); 	scanf("%d",&n);  	for(i=n; i>0; i--){ 		data = i; 		push_stack(&a,data); 	}  	print_struct(); 	move(n,&a,&b,&c,&d); 	print_struct();  	clear_stack(&a); 	clear_stack(&b); 	clear_stack(&c); 	clear_stack(&d);  	return 0; }

user: user-0
content: 那是三个塔座的汉诺塔，这个题目是四个塔座的，

user: user-3
content: 百度汉诺塔，我就不写了