【发布时间】:2021-11-15 01:55:01
【问题描述】:
有 3 个堆栈 stk0、stk1 和 stk2。为了区分 push 和 pop,程序将 push0、push1 和 push2 用于 3 个堆栈,类似地使用 pop0、pop1 和 pop2。程序以 stop0、stop1 或 stop2 结束,并显示 stack0、stack1 或 stack2 的内容,然后退出。我的代码适用于所有测试用例,接受我下面提到的那个。
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<string> stk0;
stack<string> stk1;
stack<string> stk2;
while(true) {
string a;
cout<<"Give one of options: pop, push, stop\n";
cin >> a;
if(a=="push0") {
string b;
cin >> b;
stk0.push(b);
}
else if(a=="push1") {
string b;
cin >> b;
stk1.push(b);
}
else if(a=="push2") {
string b;
cin >> b;
stk2.push(b);
}
else if(a=="pop0") {
if(!stk0.empty()) {
string b = stk0.top();
stk0.pop();
cout<<"Element popped from stack 0 is: "<<b<<endl;
}
else cout<<"Underflow in stack 0\n";
}
else if(a=="pop1") {
if(!stk1.empty()) {
string b = stk1.top();
stk1.pop();
cout<<"Element popped from stack 1 is: "<<b<<endl;
}
else cout<<"Underflow in stack 1\n";
}
else if(a=="pop2") {
if(!stk2.empty()) {
string b = stk2.top();
stk2.pop();
cout<<"Element popped from stack 2 is: "<<b<<endl;
}
else cout<<"Underflow in stack 2\n";
}
else if(a=="stop0") {
while(!stk0.empty()) {
cout<<stk0.top()<<endl;
stk0.pop();
}
break;
}
else if(a=="stop1") {
while(!stk0.empty()) {
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}
else if(a=="stop2") {
while(!stk2.empty()) {
cout<<stk2.top()<<endl;
stk2.pop();
}
break;
}
}
}
当我输入时
push0 阿格拉 push1 斋浦尔 push0 勒克瑙 push2 博帕尔 push1 阿杰梅尔 push1 乌代浦 pop0 pop1 push2 Indore push0 Meerut stop1
我得到超时:监控的命令转储核心错误。
【问题讨论】: