题解 | 堆栈的使用

堆栈的使用

https://www.nowcoder.com/practice/e91982a145944ceab6bb9a4a508e0e26

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <stdio.h>
#include <stack>
using namespace std;
int main() {
    string str;
    int n;
    while (scanf("%d", &n) != EOF) {
        stack<int> st;
        for (int i = 0; i <= n; ++i) {
            char arr[1000] = { 0 };
            fgets(arr, 1000, stdin);
            str = arr;
            if (str[0] == 'A') {
                if (st.empty()) {
                    printf("E\n");
                }
                else {
                    printf("%d\n", st.top());
                }
            }
            else if (str[0] == 'P') {
                string temp = "";
                for (int i = 2; str[i] != '\0'; ++i) {
                    temp = temp + str[i];
                }
                int temp1 = stoi(temp);
                st.push(temp1);
            }
            else if (str[0] == 'O') {
                if (!st.empty()) { st.pop(); }
            }
            str = "";
        }
    }
    return 0;
}

#shit#
全部评论

相关推荐

06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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