2 条题解

  • 2
    @ 2026-7-14 10:16:19
    #include <bits/stdc++.h>
    using namespace std;
    int a[10010], head = 0, tail = -1, n, b, c;
    
    void push(int x) {
    	a[++tail] = x;
    }
    
    void pop() {
    	head++;
    }
    
    int query() {
    	return tail - head + 1;
    }
    
    bool empty() {
    	return head > tail;
    }
    
    int front() {
    	return a[head];
    }
    
    int main() {
    	cin >> n;
    	for (int j = 0; j < n; j++) {
    		cin >> b;
    		if (b == 1) {
    			cin >> c;
    			push(c);
    		} else if (b == 2) {
    			if (empty() == 1) {
    				cout << "ERR_CANNOT_POP\n";
    			} else {
    				pop();
    			}
    		} else if (b == 3) {
    			if (empty() == 1) {
    				cout << "ERR_CANNOT_QUERY\n";
    			} else {
    				cout << front() << endl;
    			}
    		} else if (b == 4) {
    			cout << query() << endl;
    		}
    	}
    	return 0;
    }
    
    
    • 1
      @ 2026-8-13 16:48:03

      #include <bits/stdc++.h> using namespace std; int a[10010], head = 0, tail = -1, n, b, c;

      void push(int x) { a[++tail] = x; }

      void pop() { head++; }

      int query() { return tail - head + 1; }

      bool empty() { return head > tail; }

      int front() { return a[head]; }

      int main() { cin >> n; for (int j = 0; j < n; j++) { cin >> b; if (b == 1) { cin >> c; push(c); } else if (b == 2) { if (empty() == 1) { cout << "ERR_CANNOT_POP\n"; } else { pop(); } } else if (b == 3) { if (empty() == 1) { cout << "ERR_CANNOT_QUERY\n"; } else { cout << front() << endl; } } else if (b == 4) { cout << query() << endl; } } return 0; }

      哈哈哈

      • 1

      信息

      ID
      12633
      时间
      1000ms
      内存
      128MiB
      难度
      3
      标签
      递交数
      271
      已通过
      84
      上传者