输入一组数字,按照书中要求,判断相邻是否连续,输出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;
}
}