关于 栈和堆

来源:互联网 发布:c语言打印布尔值 编辑:程序博客网 时间:2024/06/06 05:46

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

44 FIFOIN 1IN 2OUTOUT4 FILOIN 1IN 2OUTOUT5 FIFOIN 1IN 2OUTOUTOUT5 FILOIN 1IN 2OUTIN 3OUT

Sample Output

122112None23
这一道题就是讲栈是先进先出,而堆是先进后出
 #include<stdio.h>#include<string.h>#include<stdlib.h>#include<ctype.h>#include<iostream>#include<algorithm>#include<stack>#include<queue>#define N 2100using namespace std;#define INF 0x3f3f3f3fint a[N];char str[20], str1[20],str2[20];int main(){    int i, n, T, m;    scanf("%d", &T);    while(T--)    {        getchar();        scanf("%d %s", &n,str);        if(str[2]=='F')//先进先出        {            queue<int>A;            for(i=0;i<n;i++)            {                scanf("%s",str1);                if(str1[0]=='I')                {                    scanf("%d",&m);                    A.push(m);                }                else                {                    if(A.empty())                        printf("None\n");                    else                    {                         printf("%d\n", A.front());                         A.pop();                    }                }            }        }        else//先进后出        {            stack<int>B;            for(i=0;i<n;i++)            {                scanf("%s",str1);                if(str1[0]=='I')                {                    scanf("%d",&m);                    B.push(m);                }                else                {                    if(B.empty())                        printf("None\n");                    else                    {                        printf("%d\n", B.top());                        B.pop();                    }                }            }        }    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 做的衣柜没有门怎么办 蒸馒头熟了会瘪怎么办 3dmax贴图太大了怎么办 嘴皮边缘颜色深怎么办 嘴巴周围肤色暗沉怎么办 中奖彩票被洗了怎么办 牙龈下面长米粒肉疙瘩怎么办 书画印章盖反了怎么办 金龙鱼一个月不吃东西怎么办 罗汉鱼头撞扁了怎么办 房顶开槽埋线白色不一样怎么办 顶上灯挪位置线怎么办 马蜂窝弄掉又来怎么办 蜂窝弄掉又有怎么办 2018年小龙虾底板脏怎么办 一本分数线擦边过怎么办 玩具塑料球扁了怎么办 胶皮与海绵开了怎么办 安卓不支持flash了怎么办 看视频要加载flash怎么办 下水道管子铁皮破了怎么办 炸金花牌一样大怎么办 玩棋牌游戏输了怎么办 苹果7插耳机外放怎么办 出国种菠菜抓了怎么办 在菲做菠菜抓到怎么办 3串1中两个怎么办 微博账号封停怎么办 阴阳师账号被永久封停怎么办 寒刃2账号被禁用怎么办 输了好多钱我该怎么办 亲朋打鱼别处在玩怎么办 做糯米蛋的蛋清怎么办 水田地没耙地平怎么办 宝宝拉鸡蛋花样大便怎么办 电子琴伴奏区无旋律音怎么办 手机触摸屏摔坏了怎么办 手机充着电玩游戏卡怎么办? 4个月宝宝拉肚子怎么办 6个月宝宝上火怎么办 1月婴儿大便干燥怎么办