rain487 2022-01-26 16:12 采纳率: 40%
浏览 46
已结题

堆栈 汉诺塔的非递归实现

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() ;  
}
  • 写回答

2条回答 默认 最新

  • LYSnowy 2022-01-26 16:24
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月6日
  • 已采纳回答 1月29日
  • 创建了问题 1月26日

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持