您的位置:首页 > 产品设计 > UI/UE

(队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)

2013-10-31 15:32 633 查看
/*
* ZOJ_3210.cpp
*
*  Created on: 2013年10月30日
*      Author: Administrator
*/

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 110;

int main(){
int a[maxn];

int t;
scanf("%d",&t);

while(t--){
bool isstack = true,isqueue = true;
int n;
scanf("%d",&n);

int i;
for(i = 0 ; i < n ; ++i){
scanf("%d",&a[i]);
}

for(i = 0 ; i < n ; ++i){
int b;
scanf("%d",&b);

if( b != a[i]){//判断是否满足先进先出
isqueue = false;
}

if(b != a[n - 1 - i]){//判断是否满足先进后出
isstack = false;
}
}

if(isstack && isqueue){
printf("both\n");
}else if(!isstack && !isqueue){
printf("neither\n");
}else if(isstack){
printf("stack\n");
}else if(isqueue){
printf("queue\n");
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: