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.
男孩被绑架了!!
他非常想念他母亲,现在很害怕。你无法想象他被关进的房间有多黑暗,如此可怜: (。
作为一个聪明的ACMer,你想让ACboy走出怪物的迷宫。但是当你到达迷宫门口时,主教说:“我听说你很聪明,但是如果不能解决我的问题,你会和一个男孩一起死去。"
墙上显示了怪物的问题:
每个问题的第一行是整数N (命令的数量),以及单词“FIFO”或“FILO”。(你很高兴,因为你知道“FIFO”代表“先进先出”,而“FILO”代表“先进先出”)。
下面的N行中,每一行都是“IN M”或“OUT”( M代表整数)。
一个问题的答案是一扇门的通道,所以如果你想拯救一个男孩,请仔细回答这个问题!
投入
输入包含多个测试用例。
第一行有一个整数,代表最常见的情况。
和每个子问题的输入如上所述。
输出
对于每个命令“OUT”,您应该根据单词“FIFO”或“FILO”输出一个整数,或者如果没有任何整数,则输出一个单词“None”。
题意即为 :① 输入数字 t 即 IN或OUT 共t个操作 FIFO(先入先出)或FILO(先入后出)
② IN 数字 或 OUT
根据前面操作 得到输出
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 <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a[10000],b[10000];
char x[10],y[10];
int main()
{
int t;
scanf("%d",&t); //t个例子;
while(t--)
{
int n, m;
int i1, i2, i3;
scanf("%d%s",&n,x); //n个操作,x 先入先出或先入后出;
if(!strcmp(x,"FILO"))//如果为先入后出
{
i1=i2=0;
while(n--)
{
scanf("%s",y); //输入字符y;
if(!strcmp(y,"IN")) //如果y为IN,则输入数字m, 并将数字m放入数组a[]中;
{
scanf("%d",&m);
a[i1++]=m;
}
else //如果y为OUT,
{
if(i1==0) //如果i1=0,即说明a[]数组里没有存放m,输出None;
printf("None\n");
else //否则输出刚才也就是最后 存入的m,即a[I-1],且在输出后记得i1--;
{
printf("%d\n",a[i1-1]);
i1--;
}
}
}
}
else if(!strcmp(x,"FIFO"))//如果为先入先出;
{
i1=i2=0; //i2用于标记数组;
while(n--)
{
scanf("%s",y); //输入字符y;
if(!strcmp(y,"IN")) //如果y为IN,则输入数字m, 并将数字m放入数组b[]中,从b[0]开始;
{
scanf("%d",&m);
b[i1++]=m; //存入一个,i1就指向下一个即将存入m的位置;
}
else //如果y为OUT,
{
if(i2>=i1) //如果i2>=i1,即i2指向了i1还未指到的地方,还未存入数;
printf("None\n");
else //输出b[i2],从零开始,每输出一次,i2++;
printf("%d\n",b[i2++]);
}
}
}
}
return 0;
}
补充:
库函数 strcmp:
if(strcmp(string 1, string 2)==0)
如果两者相同,则返回零;
如果string 1 的ASCII码先出现较大者,则返回1,否则返回零。