The first review of the finale: stack and queue (1) stack application

To put it simply, the stack can only enter and cannot exit, and the queue can enter and exit. I think the following questions can better reflect their essence, and I will definitely add them in the future!

PS: I use an array to simulate the cute new ones

Stack: Example 1: http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2709/pid/2131

#include <iostream>//It looks like a tiger at the beginning, but it has nothing to do with the priority queue, but this question is a very typical example
#include<bits/stdc++.h>//As long as you master the remainder of the stack, The realization of the loop is the new n after division, and the final output can be reversed
using namespace std;
int main()
{     int n,r,a[100000],top=0,i;     scanf("%d",&n) ;     scanf("%d",&r);     if(n==0)     {         printf("%d\n",n);     }     else     {         while(n)         {             a[++top]=n%r;             n=n/r;         }         for(i=top; i>=1; i--)         {             printf("%d",a[i]);         }         printf("\n");     }     return 0; }






















例题2:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2709/pid/3335

#include<stdio.h>//This question is a representative list of several operations, but you still need to review the question and review what operations it allows you to perform. Each set of output experimental data needs a blank line
#include<stdlib .h>
#include<string.h>
int a[1100];
char c[20];
int main()
{     int t;     int m,n,top,i,d;     scanf("%d",&t) ;     while(t--)     {         top=0;         scanf("%d %d",&m,&n);         for(i=1;i<=n;i++)         {             scanf("%s",c);             if(strcmp(c,"A")==0)             {                 if(top==0)                 {                     printf("E\n");                 }                 else                 {


















                    printf("%d\n",a[top]);
                }
            }
            else if(strcmp(c,"P")==0)
            {
                scanf("%d",&d);
                if(top>=m)
                {
                    printf("F\n");
                }
                else
                {

                    a[++top]=d;
                }
            }
            else if(strcmp(c,"O")==0)
            {                 if(top==0)                 {                    printf("E\n");                 }                 else                 {                     printf(" %d\n",a[top]);                     --top;                 }             }         }             printf("\n");     }     return 0; } Example 3: Find the next maximum value: http://acm.sdut.edu .cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2709/pid/3332















, Http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2709/pid/3333 Both questions are written in stack

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int t,n,m;
    int a[1010],b[1010];
    int top,top1;
    scanf("%d",&t);
    for(int j=1;j<=t;j++)
    {
        top=0,top1=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&m);
            a[++top]=m;
        }
        for(int i=1;i<=top;i++)
        {
            int f=0;
            for(int t=i;t<=top;t++)
            {
                if(a[t]>a[i])
                {
                    b[++top1]=a[t];
                    f=1;
                    break;
                }
            }
            if(f==0)

              b[++top1]=-1;
        }
        for(int i=1;i<=top;i++)
        {
            printf("%d-->%d\n",a[i],b[i]);
        }
        if(j!=t)

            printf("\n");
    }
    return 0;
}

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44067773/article/details/87857850