题解 | #堆栈的使用#
堆栈的使用
https://www.nowcoder.com/practice/e91982a145944ceab6bb9a4a508e0e26
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
int main() {
int n,temp;
stack<int> st;
string str;
while (scanf("%d",&n) != EOF)
{ // 注意 while 处理多个 case
while(n > 0)
{
cin >> str; //用于赋值字符串
if(str == "O")
{
if(!st.empty()) //注意,栈空下POP会出现段错误
{
st.pop();
}
}
if(str == "P")
{
scanf("%d",&temp);
st.push(temp);
}
if(str == "A")
{
if (st.empty())
{
printf("E\n");
// printf("\n");
}else
{
printf("%d\n",st.top());
// printf("\n");
}
}
n--;
}
}
}
// 64 位输出请用 printf("%lld")
基恩士成长空间 419人发布