题解 | 堆栈的使用

堆栈的使用

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#
全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务