当前位置: 首页 > news >正文

栈练习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!\n");
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!\n");return 0;}
15             top--;
16         }
17     }
18     if(!top) printf("impossible!\n");
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!\n");return 0;}
15             top--;
16         }
17         if(b==3) cout<<stack[top-1]<<endl;
18     }
19     if(!top) printf("impossible!\n");
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]

转载于:https://www.cnblogs.com/J-william/p/6194573.html

相关文章:

  • DOS引导盘制作
  • 从IaaS到AI,马云为何让阿里云去扛人工智能大旗?
  • 笔记本安装Vista/xp双系统详解
  • SpringMVC 统一返回JSON格式数据到前端
  • 如何在EditPlus、UltraEdit中正常显示韩文
  • java中关于继承的问题
  • 中文搜索引擎技术揭密:网络蜘蛛
  • 体检注意事项
  • 被感动....
  • OVS 中的各种网络设备 - 每天5分钟玩转 OpenStack(128)
  • 部署第一个域:Active Directory系列之二
  • 后台传个json到前台去老是中文乱码
  • ArcGIS.Server.9.3和ArcGIS API for JavaScript实现语句查询QueryTask(八)
  • 数据存储之SharedPreferences
  • 打印服务器
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 「面试题」如何实现一个圣杯布局?
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • co.js - 让异步代码同步化
  • Java 内存分配及垃圾回收机制初探
  • Javascript 原型链
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • k8s 面向应用开发者的基础命令
  • STAR法则
  • tab.js分享及浏览器兼容性问题汇总
  • 成为一名优秀的Developer的书单
  • 普通函数和构造函数的区别
  • 嵌入式文件系统
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 如何用vue打造一个移动端音乐播放器
  • ionic入门之数据绑定显示-1
  • 阿里云服务器购买完整流程
  • 阿里云移动端播放器高级功能介绍
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ​2020 年大前端技术趋势解读
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #mysql 8.0 踩坑日记
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (02)Hive SQL编译成MapReduce任务的过程
  • (LeetCode) T14. Longest Common Prefix
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .sh 的运行
  • @Autowired标签与 @Resource标签 的区别
  • [Android Studio] 开发Java 程序
  • [C/C++]数据结构 深入挖掘环形链表问题
  • [Java] 什么是IoC?什么是DI?它们的区别是什么?
  • [JavaEE] 线程与进程的区别详解
  • [jquery]this触发自身click事件,当前控件向上滑出
  • [Linux] MySQL数据库之索引
  • [luogu4162 SCOI2009] 最长距离(最短路)