A - I
2016-07-25 20:29
435 查看
A - I
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uSubmit
Status
Description
ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster’s labyrinth.But when you arrive at the gate of the maze, the monste say :” I have heard that you are very clever, but if can’t solve my problems, you will die with ACboy.”
The problems of the monster is shown on the wall:
Each problem’s first line is a integer N(the number of commands), and a word “FIFO” or “FILO”.(you are very happy because you know “FIFO” stands for “First In First Out”, and “FILO” means “First In Last Out”).
and the following N lines, each line is “IN M” or “OUT”, (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
Output
For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.
Sample Input
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT
Sample Output
1
2
2
1
1
2
None
2
3
题意:栈与队列的基本运用。
思路:用栈和队列解决。
失误:没有清空栈和队列。
代码如下:
#include<iostream> #include<cstdio> #include<queue> #include<stack> #include<cstring> #include<functional> using namespace std; int main() { int t,i,n,elem; char str[39],str1[89],str2[99]; queue<int> qu; stack<int> sta; cin>>t; while(t--) { cin>>n>>str;//输入字符串时越过空格读空格后的字符直到读到另一空格 然后在str后置'\0'. //cin.getline可以读取空格,和gets的用法相似无限读取 for(i=1;i<=n;++i) { if(strcmp(str,"FIFO")==0) { cin>>str1; if(strcmp(str1,"IN")==0) { cin>>elem; qu.push(elem); } else { if(qu.empty()) cout<<"None"<<endl; else { cout<<qu.front()<<endl; qu.pop(); } } } else { cin>>str2; if(strcmp(str2,"IN")==0) { cin>>elem; sta.push(elem); } else { if(sta.empty()) cout<<"None"<<endl; else { cout<<sta.top()<<endl; sta.pop(); } } } } while(!qu.empty()) //清空栈和队列 qu.pop(); while(!sta.empty()) sta.pop(); } return 0; }
相关文章推荐
- poj3264
- HDU2089 不要62
- Map 怎么排序
- 嵌入式开发13天(开发板环境搭建,开发板重装系统)
- 电子秤原理功能简介
- 第二章 模型评估与选择
- 【Maven用户手册】约定优于配置
- iptables使用简介
- POJ 1062 昂贵的聘礼 [最短路径 SPFA]
- tesseract-ocr了解
- ajax 传递数组参数
- 收发邮件的礼仪
- Java 思维导图
- H5学习之7 canvas的运用2 画圆
- 关于eclipse升级ADT的问题(没有解决)
- 利用docker-compose实现elk+redice的但宿主机环境搭建
- Linux的历史命令重用及环境的配置文件
- 【Maven用户手册】Maven之pom.xml配置文件详解
- Java 父类和子类
- 蓝点通用管理系统V13版发布了!