题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <iostream> #include <vector> using namespace std; /*push指令输入的时候,题解的输入需要两个cin,不能合并在一起 */ int main() { int a, b; int n; cin >> n;//输入栈的长度 vector<int> stack; for(int i =0;i < n;i++) { string opt;//输入操作指令 int num; cin >> opt; if(opt == "push") { cin >> num; stack.push_back(num); } else if(stack.size() != 0) { if(opt == "pop") { cout << stack.back() << endl; stack.pop_back(); } else if(opt == "top") { cout << stack.back() << endl; } } else if(stack.size() == 0) { cout << "error" << endl; } } // while (cin >> a >> b) { // 注意 while 处理多个 case // cout << a + b << endl; // } } // 64 位输出请用 printf("%lld")