33分求助

B3614 【模板】栈

vscdr_ @ 2024-10-19 11:06:45

真的不知道错哪了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    cin.tie(0),cout.tie(0);
    int t; cin >> t;
    while (t--)
    {
        stack<unsigned long long> stk;
        int n; cin >> n;
        string s;
        while (n-- && cin >> s)
        {
            if (s == "push")
            {
                int x; cin >> x;
                stk.push(x);
            }
            else if (s == "query") //输出栈顶元素
            {
                if (stk.empty())
                {
                    cout << "Anguei!" << '\n';
                }
                else
                {
                    cout << stk.top() << '\n';
                }
            }
            else if (s == "size")//输出长度
            {
                cout << stk.size() << '\n';
            }
            else if (s == "pop") //弹出一个元素
            {
                if (stk.empty())
                {
                    cout << "Empty" << '\n';
                }
                else {
                    stk.pop();
                }
            }

        }
    }

    return 0;
}

by Allmypeople @ 2024-10-19 11:15:20

@vscdr_

每查询一次把栈清零

while(!st.empty()) st.pop();

by luohoujunyang @ 2024-10-19 11:20:16

@vscdr_

int x; cin >> x; 改为 unsigned long long x; cin >> x;


by vscdr_ @ 2024-10-19 11:29:35

ac了,感谢大佬的帮助


|