堆栈 汉诺塔的非递归实现

img


img


问题应该出在这里了,这样定义有什么不对吗?
跟着网上的代码,写了一部分


#include<stdio.h>
#include<stack>
char s[4]={'0','a','b','c'};
stack<int> a[4];//栈数组 

void move(int now,int next);
int main(){
    int n;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++){
        a[1].push(n-i); 
    }
    if(n%2==1){
        s[2]='c';
        s[3]='b';
    }
    while(1){
        int next;
        for(i=1;i<=3;i++){//下一个杆 
            if(!a[i].empty() ){//为空 
                if(a[i].top() ==1){
                    if(i==3)next=1;
                    else next=i+1;
                    move(i,next);
                    break;
                }
            }
            if(a[2]==n||a[3])==n;break;
            int other1,other2;
            switch(next){
                case 1:other1=2,other2=3;break;
                case 2:other1=3,other2=1;break;
                case 3:other1=1,other2=2;break;
            }
            if(a[other1].empty() ){
                move(other2,other1);
            }
            else if(a[other2].empty() ){
                move(other1,other2);
            }
            else{
                if(a[other1]>a[other2]){
                    move(other2,other1);
                }
                else move(other1,other2);
            }
        }
    }
    
    return 0;
}
void move(int now,int next){//柱子标号 
    a[next].push(a[now].top() );
    printf("%c->%c",s[now],s[next]);
    a[now].pop() ;  
}

c语言是不支持stl库的,c++才可以使用stl,才可以include stack

你试试在3和4行中间插入:
using namespace std;