NPOJ 1086 奇怪包包

来源:互联网 发布:网络水晶头什么牌子好 编辑:程序博客网 时间:2024/06/08 17:05

题目大意:中文

注释代码:

/*                                                     * 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;intmain() {intn;//每个测例有多少个操作intcmd;//操作类型(1还是0)intx;//操作数intiscn;//flag of stack and queue//表示该结构目前是stakc还是queue//初始化为true//如果过程中发现不符合两种数据结构的规范则置为falseboolf_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();elsef_stk = false;if ( f_que )//故技重施if ( !que.empty() && x == (int)que.front() )que.pop();elsef_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;intmain() {intn;intcmd;intx;intiscn;boolf_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();elsef_stk = false;if ( f_que )if ( !que.empty() && x == (int)que.front() )que.pop();elsef_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
原创粉丝点击