首页 > 试题广场 >

堆栈的使用

[编程题]堆栈的使用
  • 热度指数:19628 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。其中 push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。(注:本题有多组输入,可以参考该网址:https://www.nowcoder.com/discuss/276

输入描述:
    对于每组测试数据,第一行是一个正整数 n(0 < n <= 10000)。而后的 n 行,每行的第一个字符可能是'P'或者'O'或者'A';如果是'P',后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O',表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽略本次操作;如果是'A',表示询问当前栈顶的值,如果当时栈为空,则输出'E'。堆栈开始为空。


输出描述:
    对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的'A'操作,输出当时栈顶的值,每个占据一行,如果当时栈为空,则输出'E'。
示例1

输入

3
A
P 5
A
4
P 3
P 6
O
A

输出

E
5
3
头像 在考古的小鱼干很有气魄
发表于 2023-03-12 11:22:59
#include <iostream> #include <stack> using namespace std; int main(){ int n; string str,data; while(cin>>n){ stack<string 展开全文
头像 Perceive109
发表于 2023-01-20 14:39:32
语言:C++11方法:库函数stack的基本操作 #include "iostream" #include "stack" using namespace std; int main() { // I/O 加速 std::ios::sync_with_stdio(false); 展开全文
头像 猫和老鼠之张鱼小丸子
发表于 2024-03-04 21:55:37
#include <iostream> #include <stack> using namespace std; int main() { int n; while (cin >> n) { stack<int> s 展开全文
头像 牛牛客小菜
发表于 2021-08-20 17:23:21
#include<iostream> #include<cstdio> #include<stack> using namespace std; int main() {     int n;   &nb 展开全文
头像 用户抉择
发表于 2021-03-18 10:40:20
#include<iostream> #include<stack> using namespace std; int main() {     int n,c=0,x;    展开全文
头像 牛客652687585号
发表于 2022-02-18 11:00:37
#include<iostream> #include<cstdio> #include<stack> using namespace std; int main(){     stack< 展开全文
头像 友人帐在逃妖怪
发表于 2021-02-18 10:22:20
改了好几次发现格式错误的地方 呜呜呜 #include <iostream> #include <cstdio> #include <stack> using namespace std; int main() { int n,m; char c; 展开全文
头像 ysong想养只修狗
发表于 2023-05-05 18:26:36
#include <iostream> #include <stack> using namespace std; stack<int> stk; int main() { int n; while (cin >> n) { 展开全文
头像 着力登峰
发表于 2023-08-09 10:43:40
#include<iostream> #include<stack> #include<map> #include<string> using namespace std; //习题5.1 堆栈的使用 /* 对于每组测试数据,第一行是一个正整数 n 展开全文
头像 窝在小角落里刷题
发表于 2023-02-18 16:04:27
#include <iostream> #include <cstdio> #include <stack> using namespace std; /** * 堆栈的使用 * @return(756076230) */ int main() { 展开全文

问题信息

难度:
93条回答 8225浏览

热门推荐

通过挑战的用户

查看代码