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

/*
 * 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
");
		}else if(!isstack && !isqueue){
			printf("neither
");
		}else if(isstack){
			printf("stack
");
		}else if(isqueue){
			printf("queue
");
		}
	}

	return 0;
}


原文地址:https://www.cnblogs.com/keanuyaoo/p/3400181.html