#1AC #2WA #3WA

B3614 【模板】栈

chenhany @ 2024-10-07 20:19:10

我用的STL

#include<bits/stdc++.h>
using namespace std;
int main() {
    string a;
    stack<int> b;
    int c;
    int n, m;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> m;
        for (int j = 0; j < m; j++) {
            cin >> a;
            if (a == "push") {
                cin >> c;
                b.push(c);
            } else if (a == "pop") {
                if (b.empty()) {
                    cout << "Empty" << endl;
                } else {
                    b.pop();
                }
            } else if (a == "query") {
                if (b.empty()) {
                    cout << "Anguei!" << endl;
                } else {
                    cout << b.top() << endl;
                }
            } else if (a == "size") {
                cout << b.size() << endl;
            }
        }
    }
    return 0;
}

by hsr_ray_kkksc03 @ 2024-10-07 20:29:58

long long


by chenhany @ 2024-10-07 20:38:51

@hsr_ray_kkksc03 不行

#include<bits/stdc++.h>
using namespace std;
int main() {
    string a;
    stack<long long> b;
    long long c;
    long long n, m;
    cin >> n;
    for (long long i = 0; i < n; i++) {
        cin >> m;
        for (long long j = 0; j < m; j++) {
            cin >> a;
            if (a == "push") {
                cin >> c;
                b.push(c);
            } else if (a == "pop") {
                if (b.empty()) {
                    cout << "Empty" << endl;
                } else {
                    b.pop();
                }
            } else if (a == "query") {
                if (b.empty()) {
                    cout << "Anguei!" << endl;
                } else {
                    cout << b.top() << endl;
                }
            } else if (a == "size") {
                cout << b.size() << endl;
            }
        }
    }
    return 0;
}

代码没过


by Ryan888 @ 2024-10-08 22:00:45

@chenhany 已调AC

#include<bits/stdc++.h>
using namespace std;
int main() {
    string a;
    unsigned long long int c;
    long long n, m;
    cin >> n;
    for (long long i = 0; i < n; i++) {
        cin >> m;
        stack<unsigned long long int> b;//每次都要清空 
        for (long long j = 0; j < m; j++) {
            cin >> a;
            if (a == "push") {
                cin >> c;
                b.push(c);
            } else if (a == "pop") {
                if (b.empty()) {
                    cout << "Empty" << endl;
                } else {
                    b.pop();
                }
            } else if (a == "query") {
                if (b.empty()) {
                    cout << "Anguei!" << endl;
                } else {
                    cout << b.top() << endl;
                }
            } else if (a == "size") {
                cout << b.size() << endl;
            }
        }
    }
    return 0;
}

求壶关


|