66分WA了一个点 求看看哪里有问题

B3614 【模板】栈

xinchenxiao @ 2024-09-26 12:28:51

因为不知道为什么没办法下载输入数据 所以无从检查 各路大佬帮忙看看

#include <iostream>
#include <stack>
#include <string>
using namespace std;

stack<unsigned long long int> a;

void c(int n) {
    for (int i = 0; i < n; i++) {
        string operation;
        cin >> operation;
        if (operation == "push") {
            unsigned long long int x;
            cin >> x;
            a.push(x);
        }
        else if (operation == "pop") {
            if (a.empty()) {
                cout << "Empty" << endl;
            }
            else {
                a.pop();
            }
        }
        else if (operation == "query") {
            if (a.empty()) {
                cout << "Anguei!" << endl;
            }
            else {
                cout << a.top() << endl;
            }
        }
        else {
            cout << a.size() << endl;
        }
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        c(n);
    }

    return 0;
}

by GXZJQ @ 2024-09-26 12:35:29

@xinchenxiao 这个是多个测试点。每次处理完一组数据要把栈清空。

#include <iostream>
#include <stack>
#include <string>
using namespace std;

stack<unsigned long long int> a;

void c(int n) {
    for (int i = 0; i < n; i++) {
        string operation;
        cin >> operation;
        if (operation == "push") {
            unsigned long long int x;
            cin >> x;
            a.push(x);
        }
        else if (operation == "pop") {
            if (a.empty()) {
                cout << "Empty" << endl;
            }
            else {
                a.pop();
            }
        }
        else if (operation == "query") {
            if (a.empty()) {
                cout << "Anguei!" << endl;
            }
            else {
                cout << a.top() << endl;
            }
        }
        else {
            cout << a.size() << endl;
        }
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        c(n);
          while(!a.empty()) a.pop();
    }

    return 0;
}

by xinchenxiao @ 2024-09-26 12:53:36

@GXZJQ 感谢 我以为会全部pop了才下一组了呜呜呜


|