#include<stdio.h>
void move(char a,char c,int n){
printf("第%d个盘从%c->%c\n",n,a,c);
}
void han(char a,char b,char c,int n){
if(n==1)move(a,c,1);
else{
han(a,c,b,n-1);
move(a,c,n);
han(b,a,c,n-1);
}
}
int main(){
int n=0;
printf("总数:");
scanf("%d",&n);
han('a','b','c',n);
}
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#define Max 50
typedef struct{
int *data;
int length;
int Max_size;
int top;
int base;
}Sqlist;
void initlist(Sqlist &L){
L.data=(int*)malloc(sizeof(int)*Max);
L.length=0;
L.Max_size=Max;
if(L.data!=0)printf("init_success!\n");
else printf("init_error!\n");
L.top=-1;
L.base=-1;
}
int pop(Sqlist &L){
int e=-1;
if(L.top==L.base)printf("empty!!");
else{
e=L.data[L.top];
L.top--;
L.length--;
}
return e;
}
void push(Sqlist &L,int e){
if(L.length>=L.Max_size)printf("overflow!!");
else{
L.top++;
L.data[L.top]=e;
L.length++;
}
}
void han(Sqlist &L1,Sqlist &L2,Sqlist &L3,int n){
if(n==1){
int i=pop(L1);
push(L2,i);
}
else{
han(L1,L3,L2,n-1);
int j=pop(L1);
push(L2,j);
han(L3,L2,L1,n-1);
}
}
void input(Sqlist &L,int count){
printf("please input list for A柱:");
for(int i=0;i<count;i++){
int j;
scanf("%d",&j);
push(L,j);
}
}
void print(Sqlist &L,int count){
for(int i=0;i<count;i++){
int j;
j=pop(L);
printf("%d\t",j);
}
printf("\n");
}
int main(){
Sqlist L1,L2,L3;//声明一个线性表
initlist(L1);//线性表的第一次初始化,为线性表分配存储空间
initlist(L2);
initlist(L3);
printf("already initlist\n");
printf("how long :");
int count;
scanf("%d",&count);
int n=count;
input(L1,count);
han(L1,L2,L3,n);
print(L2,count);
}