• A


      

    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!

    InputThe 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.OutputFor 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

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #define STACK_INIT_SIZE 50//储存空间初始分配量
      5 #define STACKINCREMENT  10//存储空间分配增量
      6 #define Queue_MAX_SIZE 50
      7 #define OK 1
      8 #define ERROR 0
      9 typedef int QueueType; //队列元素类型
     10 typedef int StackType; //栈元素类型
     11 
     12 typedef struct
     13 {
     14     QueueType *pBase;    //队列指针
     15     QueueType front;     //队头索引
     16     QueueType rear;      //队尾索引
     17     int maxSize;         //当前分配最大容量
     18 }Queue;
     19 //队列的初始化
     20 int InitQueue(Queue *p)
     21 {
     22     p->pBase = (QueueType *)malloc(Queue_MAX_SIZE * sizeof(QueueType));
     23     if (p->pBase == NULL) return ERROR; //内存分配失败
     24     p->front = 0;
     25     p->rear = 0;     //初始化 队头队尾索引均为0
     26     p->maxSize = Queue_MAX_SIZE;
     27     return 0;
     28 }
     29 //销毁队列
     30 void DestroyQueue(Queue *p)
     31 {
     32     free(p);
     33     p = NULL;
     34 
     35 }
     36 //清空队列
     37 void ClearQueue(Queue *p)
     38 {
     39     p->front = 0;
     40     p->rear = 0;
     41 }
     42 //判断队列是否为空
     43 int IsEmpityQueue(Queue *p)
     44 {
     45     if (p->front == p->rear)
     46         return OK;
     47     return ERROR;
     48 
     49 }
     50 
     51 //判断队列是否满
     52 int IsFullQueue(Queue *p)
     53 {
     54     if ((p->rear + 1) % p->maxSize == p->front)
     55         return OK;
     56     return ERROR;
     57 
     58 } 
     59 //新元素入队
     60 int EnQueue(Queue *p, QueueType e)
     61 {
     62     if (IsFullQueue(p) == OK)
     63     {
     64         printf("队列已满
    ");
     65         return ERROR;
     66     }
     67     p->pBase[p->rear] = e;
     68     p->rear = (p->rear + 1) % p->maxSize;
     69     return OK;
     70 }
     71 //队头元素出列
     72 int DeQueue(Queue *p, QueueType *pe)
     73 {
     74     //如果队列为空 则返回ERROR
     75     if (IsEmpityQueue(p) == OK)
     76         return ERROR;
     77 
     78 
     79     *pe = p->pBase[p->front];
     80     p->front = (p->front + 1) % p->maxSize;
     81 
     82     return OK;
     83 }
     84 
     85 typedef struct {
     86     StackType *base;   //在构造之前和销毁之后,base的值为NULL
     87     StackType *top;    //栈顶指针
     88     int stacksize;     //当前已分配的存储空间,以元素为单位
     89 }SqStack; //顺序栈
     90 
     91 //栈的初始化
     92 int InitStack(SqStack *p)
     93 {
     94     p->base = (StackType*)malloc(STACK_INIT_SIZE * sizeof(StackType));
     95     if (p->base == NULL)  return ERROR;  //内存分配失败
     96     p->top = p->base;     //栈顶与栈底相同表示一个空栈
     97     p->stacksize = STACK_INIT_SIZE;
     98     return OK;
     99 
    100 }
    101 //判断栈是否为空
    102 int EmptyStack(SqStack *p) {
    103     //若为空栈 则返回OK,否则返回ERROR
    104     if (p->top == p->base) return OK;
    105     else 
    106         return ERROR;
    107 }
    108 //顺序栈的压入
    109 int Push(SqStack *p, StackType e) {
    110     //插入元素e为新的栈顶元素
    111     if ((p->top - p->base) >= p->stacksize)   //栈满,追加储存空间
    112     {
    113         p->base = (StackType*)realloc(p->base, (p->stacksize + STACKINCREMENT) * sizeof(StackType));
    114         if (p->base == NULL)   return ERROR;// 储存空间分配失败
    115         p->top = p->base + p->stacksize;    //可能有人觉得这句有点多余(我当时也是这么想的 后面有解释)
    116         p->stacksize += STACKINCREMENT;
    117     }
    118     *(p->top) = e;
    119     (p->top)++;
    120     return OK;
    121 }
    122 // 顺序栈的弹出     
    123 int Pop(SqStack *p, StackType *e) {
    124     //若栈不空,则删除p的栈顶元素,用e返回其值
    125     if (EmptyStack(p) == OK) 
    126         return ERROR;
    127     
    128     --(p->top);
    129     *e = *(p->top);
    130     return OK;
    131 
    132 
    133 }
    134 //顺序栈的销毁
    135 int DestroyStack(SqStack *p) {
    136     //释放栈底空间并置空
    137     free(p->base);
    138     p->base = NULL;
    139     p->top = NULL;
    140     p->stacksize = 0;
    141 
    142     return OK;
    143 }
    144 //将顺序栈置空 栈还是存在的,栈中的元素也存在,如果有栈中元素的地址任然能调用
    145 int ClearStack(SqStack *p) {
    146     p->top = p->base;
    147     return OK;
    148 }
    149 
    150 
    151 
    152 int main()
    153 {
    154     int result[100],k=0;
    155     StackType *se, st;
    156     SqStack *pstack, stack;
    157     pstack = &stack;
    158     se = (StackType*)malloc(sizeof(StackType));    //为指针se分配内存地址
    159     
    160     QueueType *qe = (QueueType*)malloc(sizeof(QueueType)),qt;
    161     Queue *pQueue = (Queue *)malloc(sizeof(Queue));
    162     InitStack(pstack);                            //初始化栈
    163     InitQueue(pQueue);    //初始化队列
    164     int x,y;
    165     char flag[5],handle[5];
    166     scanf("%d", &x);
    167     for (int i = 0; i < x; i++)
    168     {
    169         scanf("%d", &y);
    170         scanf("%s", flag);
    171         for (int j = 0; j < y; j++)
    172         {   
    173             if (strcmp(flag, "FIFO")==0)   //队列
    174             {
    175                 scanf("%s", handle);
    176                 if (strcmp(handle, "IN") == 0) { scanf("%d", &qt); EnQueue(pQueue, qt); }
    177                 if (strcmp(handle, "OUT") == 0) 
    178                 {
    179                     if(DeQueue(pQueue, qe)==OK) result[k++]=*qe; 
    180                     else result[k++] = 99; //99是一个标记
    181                 
    182                 }
    183                 
    184             }
    185 
    186             if (strcmp(flag, "FILO") == 0)     //
    187             {
    188                 scanf("%s", handle);
    189                 if (strcmp(handle, "IN") == 0) { scanf("%d",&st ); Push(pstack, st); }
    190                 if (strcmp(handle, "OUT") == 0) 
    191                 { 
    192                     if(Pop(pstack,se)==OK) result[k++]=*se;
    193                     else
    194                     result[k++] = 99;
    195                 }
    196             }
    197         }
    198         ClearStack(pstack);
    199         ClearQueue(pQueue);
    200 
    201     }
    202     result[k] = '';
    203     DestroyQueue(pQueue);
    204     DestroyStack(pstack);
    205     k = 0;
    206     while (result[k] != '')
    207     {
    208         if (result[k] != 99)
    209         printf("%d
    ", result[k]);
    210         
    211         else
    212         printf("None
    "); 
    213     
    214     k++;    
    215     }
    216     return 0;
    217 
    218 
    219 }
  • 相关阅读:
    FLEX图像工具类-图像变形
    flex中list或Combox中的子项上移下移操作
    flex中socket的使用
    Flex2款简单FLV播放器很经典
    Flex中Css参考示例
    Flex中CursorManager的应用
    关于FLEX的安全沙箱问题
    Flex实现多文件上传之一:前台部分
    Flex与JS通信
    metasploit 常用指令
  • 原文地址:https://www.cnblogs.com/mwq1024/p/10546862.html
Copyright © 2020-2023  润新知