Простий стек
Как узнать 1-ый тест?(если возможно). Есть ли на входе отрицательные числа? Есть ли в тестах после n( т.е. после push n) пробел?
що не так з #2391274 не проходить перший тест
include <iostream>
include <deque>
using namespace std;
int main() { deque<int> d; string c; while (cin >> c) { if (c == "push") {int n; cin >> n; d.push_back(n); cout << "ok\n";} else if (c == "pop") {if (d.empty()) cout << "error\n"; else {cout << d.back() << "\n"; d.pop_back();}} else if (c == "back") {if (d.empty()) cout << "error\n"; else cout << d.back() << "\n";} else if (c == "size") cout << d.size() << "\n"; else if (c == "clear") {d.clear(); cout << "ok\n";} else if (c == "exit") {cout << "bye\n"; break;} } return 0; }
// #16369901
include <iostream>
include <stack>
using namespace std;
int main()
{ stack<int> st;
int k;
string command;
while(true)
{
cin >> command;
if(command=="push")
{ cin >> k;
st.push(k);
cout << "ok\n";
}
else
if(command =="pop")
{
cout << st.top()<<endl;
st.pop();
}
else
if(command =="back")
cout <<st.top()<< endl;
else
if(command =="size")
cout <<st.size()<< endl;
else
if(command =="exit")
{
cout <<"bye"<< endl;
break;
}
else
if(command=="clear")
{
cout <<"ok\n";
while(!st.empty()) {st.pop();}
}
}
return 0;
}