1、栈

(1)栈的模拟

特点:先进后出

eg1:火车进站

实际就是模拟一个栈

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int Stack[maxn],a[maxn],stack[maxn],n,l=1;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    for(int i=1;i<=n;i++)
    {
        while(a[i]>=l)
        {
            Stack[++n]=l;
            l++;
        }
        if(Stack[n]==a[i])
        {
            n--;
        }
        else
        {
            printf("Wrong!");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;
 } 
火车进栈

相关文章:

  • 2021-11-23
  • 2022-12-23
  • 2022-02-28
  • 2021-04-03
  • 2022-02-03
  • 2021-07-11
  • 2022-03-08
猜你喜欢
  • 2021-07-21
  • 2021-05-19
  • 2021-12-18
  • 2021-10-14
  • 2021-07-11
  • 2021-05-22
相关资源
相似解决方案