33?
查看原帖
33?
612058
尘浊楼主2023/9/21 21:31

33分?

大佬求救

/*
 * @Author: snake qzrobotsnake@gmail.com
 * @Date: 2023-08-04 20:16:50
 * @LastEditors: snake qzrobotsnake@gmail.com
 * @LastEditTime: 2023-09-21 21:27:40
 * @FilePath: \cppDemo\src\main.cpp
 */
#include <iostream>
using namespace std;
class Stack
{
  public:
    Stack(){};
    Stack(int size);
    ~Stack();
    void add(int x)
    {
        p_arr[++p_head] = x;
    }
    string pop()
    {
        if (p_head == 0)
            return "Empty";
        else
            p_head--;
        return "";
    }
    string query()
    {
        if (p_head == 0)
            return "Anguei!";
        else
            return to_string(p_arr[p_head]);
    }
    int size() { return p_head; }

  private:
    unsigned long long *p_arr;
    int p_head;
};
Stack::Stack(int size)
{
    p_head = 0;
    p_arr = new unsigned long long[size];
}
Stack::~Stack()
{
    delete[] p_arr;
}
int main()
{
    // freopen("test.out", "w", stdout);
    cin.tie(0);
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    for (int i = 0; i < T; i++)
    {
        int n;
        cin >> n;
        Stack stack(n);
        for (int i = 0; i < n; i++)
        {
            string str;
            cin >> str;
            if (str == "push")
            {
                int x;
                cin >> x;
                stack.add(x);
            }
            else if (str == "pop")
            {
                string pop = stack.pop();
                cout << pop;
                if (pop != "")
                    cout << endl;
            }
            else if (str == "query")
            {
                cout << stack.query() << endl;
            }
            else if (str == "size")
            {
                cout << stack.size() << endl;
            }
        }
    }
    return 0;
}
2023/9/21 21:31
加载中...