zoukankan      html  css  js  c++  java
  • 栈练习1,2,3

    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

    代碼實現:

    手模

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int stack[100],top,n,b,em;
     5 int main(){
     6     cin>>n;
     7     for(int i=0;i<n;i++){
     8         cin>>b;
     9         if(b==1){
    10             cin>>em;
    11             stack[top++]=em;
    12         }
    13         else top--;
    14     }
    15     if(!top) printf("impossible!
    ");
    16     else cout<<stack[top-1]<<endl;
    17     return 0;
    18 }

    STL

     1 #include<stack>
     2 #include<iostream>
     3 using namespace std;
     4 stack<int> st;
     5 int a,b,n;
     6 int main(){
     7     cin>>n;
     8     for(int i=1;i<=n;i++){
     9         cin>>a;
    10         if(a==1){
    11             cin>>b;
    12             st.push(b);
    13         }
    14         if(a==2){
    15             if(st.empty()){cout<<"impossible!";return 0;}
    16         st.pop();
    17     }
    18     }
    19     if(!st.empty()) cout<<st.top();
    20     else cout<<"impossible!";
    21     return 0;
    22 }

    2

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

    (此题与栈练习1相比改了2处:1加强了数据 2不保证栈空时不会出栈)

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

    输入描述 Input Description

    N(操作个数)

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

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

    输出描述 Output Description

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

    样例输入 Sample Input

    3

    1 2

    2

    2

    样例输出 Sample Output

    impossible!

    数据范围及提示 Data Size & Hint

    对于100%的数据  N≤100000 元素均为正整数且小于等于10^8

    代碼實現:

    手模

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int stack[100],top,n,b,em;
     5 int main(){
     6     cin>>n;
     7     for(int i=0;i<n;i++){
     8         cin>>b;
     9         if(b==1){
    10             cin>>em;
    11             stack[top++]=em;
    12         }
    13         else{
    14             if(!top){printf("impossible!
    ");return 0;}
    15             top--;
    16         }
    17     }
    18     if(!top) printf("impossible!
    ");
    19     else cout<<stack[top-1]<<endl;
    20     return 0;
    21 }

    STL

     1 #include<stack>
     2 #include<iostream>
     3 using namespace std;
     4 stack<int> st;
     5 int a,b,n;
     6 int main(){
     7     cin>>n;
     8     for(int i=1;i<=n;i++){
     9         cin>>a;
    10         if(a==1){
    11             cin>>b;
    12             st.push(b);
    13         }
    14         if(a==2){
    15             if(st.empty()){cout<<"impossible!";return 0;}
    16         st.pop();
    17     }
    18     }
    19     if(!st.empty()) cout<<st.top();
    20     else cout<<"impossible!";
    21     return 0;
    22 }

    3

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

    比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保证访问栈顶元素时或出栈时栈不为空),现在给出这N此操作,输出结果。

    输入描述 Input Description

    N

    N次操作(1入栈 2出栈 3访问栈顶)

    输出描述 Output Description

    K行(K为输入中询问的个数)每次的结果

    样例输入 Sample Input

    6

    1  7

    3

    2

    1  9

    1  7

    3

    样例输出 Sample Output

    7

    7

    数据范围及提示 Data Size & Hint

     对于50%的数据 N≤1000 入栈元素≤200

     对于100%的数据 N≤100000入栈元素均为正整数且小于等于10^4 

    代碼實現:

    手模

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 int stack[100],top,n,b,em;
     5 int main(){
     6     cin>>n;
     7     for(int i=0;i<n;i++){
     8         cin>>b;
     9         if(b==1){
    10             cin>>em;
    11             stack[top++]=em;
    12         }
    13         if(b==2){
    14             if(!top){printf("impossible!
    ");return 0;}
    15             top--;
    16         }
    17         if(b==3) cout<<stack[top-1]<<endl;
    18     }
    19     if(!top) printf("impossible!
    ");
    20     return 0;
    21 }

    STL

     1 #include<stack>
     2 #include<iostream>
     3 using namespace std;
     4 stack <int> s;
     5 int n,a,b;
     6 int main(){
     7     cin>>n;
     8     while(n--){
     9         cin>>a;
    10         if(a==1){
    11             cin>>b;
    12             s.push(b);
    13         }
    14         if(a==2) s.pop();
    15         if(a==3) cout<<s.top()<<endl;
    16     }
    17     return 0;
    18 }

    這麼水,感覺有點虛。

    题目来源:CODE[VS]

  • 相关阅读:
    Linux scp、ssh命令
    Linux ps、top、free、uname命令
    适配器模式
    Linux不能进入图形化界面运行yum不管用
    SpringMVC 测试 mockMVC
    Idea和Git集成,并且Git管理不同的秘钥,idea分别提交项目到GitLab和GitHub
    多线程
    Java中Volatile关键字详解
    Spring事务管理--(二)嵌套事物详解
    jConsole, jVisualvm, btrace 区别和联系
  • 原文地址:https://www.cnblogs.com/J-william/p/6194573.html
Copyright © 2011-2022 走看看