求助(已阅警示后人)
  • 板块灌水区
  • 楼主LGZX
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/3/28 21:43
  • 上次更新2024/3/29 12:06:26
查看原帖
求助(已阅警示后人)
1043009
LGZX楼主2024/3/28 21:43
#include<iostream>  
#include<algorithm>  
#include<cstring>  
#include<cmath>  
#include<stack>  

using namespace std;

stack<int> nums;
string command;
int num;

void clearStack(stack<int>& s) {

    while (!s.empty()) {

        s.pop();

    }

}

int main(void) {
    int T, n;
    cin >> T; 
    while (T--) {
        cin >> n;
        while (n--) {
            cin >> command;
            if (command == "push") {
                cin >> num;
                nums.push(num);
            }
            else if (command == "pop") {
                if (!nums.empty()) {
                    nums.pop();
                }
                else {
                    cout << "Empty" << endl;
                }
            }
            else if (command == "query") {
                if (!nums.empty()) {
                    cout << nums.top() << endl;
                }
                else {
                    cout << "Anguei!" << endl;
                }
            }
            else if (command == "size") {
                cout << nums.size() << endl;
            }
        }
        clearStack(nums);
    }
    return 0;
}
2024/3/28 21:43
加载中...