1044: 顺序栈基本操作的实现
解法:
#include<iostream>
#include<stack>
using namespace std;
int main() {int n, a, k;stack<int> sk;cin >> n;while (n--) {cin >> a;sk.push(a);}cin >> k;while (k--) {sk.pop();}if (!sk.empty()) {cout << sk.top();}else cout << -1;
}
解法:
#include<iostream>
#include<stack>
using namespace std;
int main() {int n, a, k;stack<int> sk;cin >> n;while (n--) {cin >> a;sk.push(a);}cin >> k;while (k--) {sk.pop();}if (!sk.empty()) {cout << sk.top();}else cout << -1;
}