题解 | #堆栈的使用#
堆栈的使用
http://www.nowcoder.com/practice/e91982a145944ceab6bb9a4a508e0e26
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
int n;
while (cin >> n) {
if (n == 0) {
break;
}
char type;
stack<int> Mystack;
int x;
for (int i = 0; i < n; i++)
{
cin >> type;
if (type == 'A') {
if (Mystack.empty()) {
cout << "E" << endl;
}
else
{
cout << Mystack.top() << endl;
}
}
else if (type == 'P') {
cin>>x;
Mystack.push(x);
}
else if (type == 'O') {
if(!Mystack.empty()){
Mystack.pop();
}
}
}
cout << "\n";
}
return 0;
}
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
int n;
while (cin >> n) {
if (n == 0) {
break;
}
char type;
stack<int> Mystack;
int x;
for (int i = 0; i < n; i++)
{
cin >> type;
if (type == 'A') {
if (Mystack.empty()) {
cout << "E" << endl;
}
else
{
cout << Mystack.top() << endl;
}
}
else if (type == 'P') {
cin>>x;
Mystack.push(x);
}
else if (type == 'O') {
if(!Mystack.empty()){
Mystack.pop();
}
}
}
cout << "\n";
}
return 0;
}