#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define endl '\n' #define nl cout << '\n' #define den(a) cout << #a << " = " << a << '\n'; #define deg(a) cout << #a << " = " << a << " "; #define yes() cout << "Yes\n" #define no() cout << "No\n" const int N = 2e5 + 10, Mod = 998244353; void solve() { int n; cin >> n; stack<int> q; while (n --) { string op; cin >> op; if(op == "push") { int x; cin >> x; q.push(x); } else if(op == "pop") { if(q.size()) { cout << q.top() << endl; q.pop(); } else cout << "error" << endl; } else { if(q.size()) { cout << q.top() << endl; } else cout << "error" << endl; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; //cin >> t; while (t--)solve(); return 0; }
直接用C++自带的stack即可