2 条题解

  • 1
    @ 2023-6-2 14:59:19

    栈是一种后进先出的数据结构,即:后进入的元素先出来,先进入的元素后出来。

    它支持以下操作:

    stack<int>s : 创建一个新栈 s。
    push(a) : 往栈里加入一个元素 a。
    pop() : 弹出末尾元素。
    empty() : 判断栈是否空了。
    top() : 返回末尾元素。
    size() : 返回元素个数。
    

    于是代码来了:

    #include<bits/stdc++.h>
    using namespace std;
    stack<unsigned long long>s;
    int main(){
        unsigned long long n,b,t;
        string a;
        cin>>t;
        while(t--){
            cin>>n;
            while(n--){
                cin>>a;
                if(a=="push"){
                    cin>>b;
                    s.push(b);
                }else if(a=="pop"){
                    if(s.empty()){
                        cout<<"Empty"<<endl;
                        continue;
                    }
                    s.pop();
                }else if(a=="query"){
                    if(s.empty()){
                        cout<<"Anguei!"<<endl;
                        continue;
                    }
                    cout<<s.top()<<endl;
                }else if(a=="size"){
                    cout<<s.size()<<endl;
                }
            }
            while(!s.empty()){
                s.pop();
            }
        }
        return 0;
    }
    
    • 0
      @ 2022-7-12 23:08:51
      #include <stack>
      #include <string>
      #include <iostream>
      
      int main() {
        std::cin.tie(0);
        std::ios::sync_with_stdio(false);
        int T, n;
        for (std::cin >> T; T; --T) {
          std::stack<unsigned long long int> s;
          for (std::cin >> n; n; --n) {
            std::string t;
            std::cin >> t;
            if (t == "push") {
              unsigned long long x;
              std::cin >> x; s.push(x);
            } else if (t == "pop") {
              if (s.empty()) std::cout << "Empty\n";
              else s.pop();
            } else if (t == "query") {
              if (s.empty()) std::cout << "Anguei!\n";
              else std::cout << s.top() << '\n';
            } else {
              std::cout << s.size() << '\n';
            }
      		}
      	}
        return 0;
      }
      
      • 1

      信息

      ID
      6765
      时间
      1000ms
      内存
      128MiB
      难度
      2
      标签
      递交数
      56
      已通过
      17
      上传者