zoukankan      html  css  js  c++  java
  • 73.3137 栈练习1

    3137 栈练习1

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 黄金 Gold
    题目描述 Description

    给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈。先给出这些操作,请输出最终栈的栈顶元素。  操作解释:1表示入栈,2表示出栈

    输入描述 Input Description

    N(操作个数)

    N个操作(如果是入栈则后面还会有一个入栈元素)

    具体见样例(输入保证栈空时不会出栈)

    输出描述 Output Description

    最终栈顶元素,若最终栈空,输出”impossible!”(不含引号)

    样例输入 Sample Input

    3

    1 2

    1 9

    2

    样例输出 Sample Output

    2

    数据范围及提示 Data Size & Hint

    对于100%的数据  N≤1000 元素均为正整数且小于等于100

    分类标签 Tags 点此展开 

    代码:
    #include
    using namespace std;
    #include
    int zhan[1001];
    int top=0;
    int main()
    {
    int n;
    cin>>n;
    int cz,s;
    for(int i=1;i<=n;++i)
    {
    scanf("%d",&cz);
    if(cz==1)
    {
    top++;
    scanf("%d",&s);
    zhan[top]=s;
    }
    if(cz==2)
    top--;
    }
    if(zhan[top])
    printf("%d ",zhan[top]);
    else printf("impossible! ");
    return 0;
    }
  • 相关阅读:
    day4-1
    day3-1
    day1-3
    day2-1
    day1-2
    day1 1
    对象的高度整合
    类和数据类型
    对象的绑定方法
    python总结
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5370746.html
Copyright © 2011-2022 走看看