数据结构----------栈的应用(课后20题)

输入一组数字,按照书中要求,判断相邻是否连续,输出1或者0。

1代表结果为真,0代表结果为假。

输入:

9 \当前栈中元素个数

4 5 -2 -3 11 10 5 6 20 \元素内容

输出:

1 \真

输入输出样例:1组
#1

样例输入:

9
4 5 -2 -3 11 10 5 6 20 

样例输出:

1
//注意
    //1:该程序每次运行的时间必须小于10秒,否则会超时,程序超时将不会测试剩余的测试集
    //2:该程序每次运行使用的内存不能超过1M,否则会返回错误
    //3:该程序每次运行输出的结果最多显示1000个字符(多余的不显示),每行末尾的所有空格用□表示
#include <iostream>
#define MAXSIZE 1024
using namespace std;

typedef struct
{
    int data[MAXSIZE];
    int top;
}SeqStack;

SeqStack *init_stack();
int push_stack(SeqStack *s,int x);
void define(SeqStack *s);
int main()
{
    int n;
    cin>>n;
    SeqStack *s;
    s=init_stack();
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        push_stack(s,x);
    }
    define(s);
    return 0;
}
//方法函数
void define(SeqStack *s)
{
    SeqStack *t;
    t=init_stack();
    while(s->top!=-1)//将栈中元素逆序存储到t中
    {
        int x;
        x=s->data[s->top];
        push_stack(t,x);
        s->top--;
    }
    int pre=1;int beh=0;
    int mid=pre-beh;
    
    while(t->top!=-1&&(mid==1||mid==-1))//判断
    {
        if(t->top==1)//栈中只有两个元素时
    	{
        	pre=t->data[t->top];
        	t->top--;
        	beh=t->data[t->top];
        	mid=pre-beh;
        	if(mid==1||mid==-1){cout<<1;return;}
        	else{cout<<0;return;}
    	}
        pre=t->data[t->top];
        t->top--;
        if(t->top!=-1)
        {
            beh=t->data[t->top];
            t->top--;
            mid=pre-beh;
        }
    }
    if(t->top==-1)//
    {
        cout<<1;
    }
    else{cout<<0;}
}
SeqStack *init_stack()
{
    SeqStack *s;
    s=new SeqStack;
    s->top=-1;
    return s;
}
int push_stack(SeqStack *s,int x)
{
    if(s->top==MAXSIZE-1) return 0;
    else{
        s->top++;
        s->data[s->top]=x;
        return 1;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值