1 条题解
-
1
栈是一种先进后出的数据结构。(吃了吐)
用数组来模拟数据结构会比 STL 快。
#include<iostream> using namespace std; const int N = 100010; int str[N], tt; int main() { string op; int m; cin >> m; while (m--) { int x; cin >> op; if (op == "push") { cin >> x; str[++tt] = x; } else if (op == "pop") --tt; else if (op == "empty") cout << (tt ? "NO" : "YES") << endl; else cout << str[tt] << endl; } return 0; }
下面为 STL 。
#include <iostream> #include <stack> #include <string> using namespace std; stack <int> s; int main() { int n; cin >> n; while(n --) { string op; cin >> op; if(op == "push") { int x; cin >> x; s.push(x); } else if(op == "pop") { s.pop(); } else if(op == "empty") { if(s.size()) cout << "NO" << '\n'; else cout << "YES" << '\n'; } else { cout << s.top() << '\n'; } } return 0; }
- 1
信息
- ID
- 76
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 8
- 标签
- 递交数
- 14
- 已通过
- 6
- 上传者