NPOJ 1086 奇怪包包

题目大意:中文

注释代码:

/*                                                    
 * Problem ID : NPOJ 1086 奇怪包包
 * Author     : Lirx.t.Una                                                    
 * Language   : C++                                  
 * Run Time   : 1                                                    
 * Run Memory : 1688                                                    
*/ 

#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

int
main() {
	
	int		n;//每个测例有多少个操作
	int		cmd;//操作类型(1还是0)
	int		x;//操作数
	
	int		iscn;
	
	//flag of stack and queue
	//表示该结构目前是stakc还是queue
	//初始化为true
	//如果过程中发现不符合两种数据结构的规范则置为false
	bool	f_stk, f_que;
	
	iscn = 0;
	while ( ~scanf("%d", &n) ) {
		
		stack<char>		stk;
		queue<char>		que;
		
		//初始化
		f_stk = true;
		f_que = true;
		
		while ( n-- ) {
			
			scanf("%d%d", &cmd, &x);
			
			if ( 1 == cmd ) {//压入操作
				
				//如果都已经不是stakc或queue了就没有必要再执行push操作了
				if ( f_stk ) stk.push((char)x);
				if ( f_que ) que.push((char)x);
			}
			else {//否则为弹出操作
				
				//!!!可能会出现已经为空但仍然弹出的情况,这种情况必然是
				//错误的,因此需要将flag值为false
				//测例中恶心的出现了这种狗血的情况!!!

				if ( f_stk )//如果仍然还是stack则要检查是否为空
					//并检查弹出元素是否符合规范
					if ( !stk.empty() && x == (int)stk.top() )
						stk.pop();
					else
						f_stk = false;
					
				if ( f_que )//故技重施
					if ( !que.empty() && x == (int)que.front() )
						que.pop();
					else
						f_que = false;
			}
		}
		
		printf("Case #%d: ", ++iscn);
		switch ( (int)f_stk + ( ( (int)f_que ) << 1 ) ) {//用两位2进制数组合4中情况
			
			case 0 :
			
				puts("impossible");
				break;
			
			case 1 :
			
				puts("stack");
				break;
			
			case 2 : 
			
				puts("queue");
				break;
			
			case 3 :
			
				puts("not sure");
				break;
			
			default : break;	
		}
	}
	
	return 0;
}
无注释代码:

#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

int
main() {

	int		n;
	int		cmd;
	int		x;

	int		iscn;

	bool	f_stk, f_que;

	iscn = 0;
	while ( ~scanf("%d", &n) ) {

		stack<char>		stk;
		queue<char>		que;

		f_stk = true;
		f_que = true;

		while ( n-- ) {
		
			scanf("%d%d", &cmd, &x);

			if ( 1 == cmd ) {
			
				if ( f_stk ) stk.push((char)x);
				if ( f_que ) que.push((char)x);
			}
			else {
			
				if ( f_stk )
					if ( !stk.empty() && x == (int)stk.top() )
						stk.pop();
					else
						f_stk = false;

				if ( f_que )
					if ( !que.empty() && x == (int)que.front() )
						que.pop();
					else
						f_que = false;
			}
		}

		printf("Case #%d: ", ++iscn);
		switch ( (int)f_stk + ( ( (int)f_que ) << 1 ) ) {
			
			case 0 :

				puts("impossible");
				break;

			case 1 :

				puts("stack");
				break;

			case 2 : 

				puts("queue");
				break;

			case 3 :

				puts("not sure");
				break;

			default : break;	
		}
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值