题解 | #堆栈的使用#

堆栈的使用

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")

全部评论

相关推荐

ResourceUt...:楼主有自己的垃圾箱,公司也有自己的人才库
点赞 评论 收藏
分享
09-12 11:55
已编辑
湖南工商大学 Java
那一天的Java_J...:这种一堆问题的,别去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务