题解 | 堆栈的使用
堆栈的使用
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#
查看22道真题和解析