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
- NPOJ 1086 奇怪包包
- 包包!
- NPOJ 1066 找气球
- NPOJ 1058 欢迎新人!
- NPOJ 1065 喵星人吃土豆
- NPOJ 1063 火柴棒拼数
- NPOJ 1069 小学生作业
- NPOJ 1067 Flappy48
- 奇怪
- 奇怪
- 奇怪
- 奇怪
- 奇怪
- 奇怪!
- 奇怪
- 奇怪
- 奇怪
- 奇怪
- iOS中获取用户所点击的位置信息,并通过这个位置信息得到tableView的中的cell的indexPath.section
- Android隐式启动Activity
- Picture Aspect Ratio
- Qt移动应用开发(五):场景切换
- 贴片电阻阻值代码对照表
- NPOJ 1086 奇怪包包
- MySQL存储过程详解 mysql 存储过程
- 机试-自守数个数统计
- js判断身份证是否合法
- 数据库sql查询语句备忘
- android根据内容对list重新排序
- Maven—Windows操作系统中安装配置Maven环境
- 新技能上Google
- ThreadPool线程池