42 条题解

  • -4
    @ 2026-8-14 17:21:19

    #include<bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int e[N], nxt[N], tot; int head = 1; // 头指针 int tail; // 尾指针 // 在链表末尾加入 x 这个值 void add(int x) { ++tot; e[tot] = x; nxt[tot] = 0; nxt[tail] = tot; tail = tot; }

    // 在链表头部加入 x void insert(int x) { ++tot; e[tot] = x; nxt[tot] = head; head = tot; }

    // 遍历链表输出 void print() { for (int i = head; i; i = nxt[i]) { cout << e[i] << endl; } }

    int main() { add(10); add(20); add(30); insert(60); print(); } #include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10; char stk[N]; int top; string s; int T; int main() { cin >> T; while (T--) { top = 0; cin >> s; bool flg = 1; // 标记是否合法 // for(int i=0;i<s.size();i++) char c = s[i] for (char c : s) { // C++ 11 范围遍历 if (c == '(' || c == '[' || c == '{') stk[++top] = c; else { // 右括号 if (top == 0) { flg = 0; break; } else if (c == ')' && stk[top] == '(' ) top--; else if (c == ']' && stk[top] == '[' ) top--; else if (c == '}' && stk[top] == '{' ) top--; else { flg = 0; break; } } } if (top) flg = 0; if (flg) cout << "YES" << endl; else cout << "NO" << endl; } return 0; } #include<bits/stdc++.h> using namespace std; const int N = 1e3 + 10; const int M = 1e6 + 10; queueq[N]; // 给每一个组别开一个队列 queue qq; // 开一个存储组别编号 int a[M]; // a[i] = j, 代表 i 属于 j 组 int n, m; string opt; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> m; for (int j = 1; j <= m; j++) { int x; cin >> x; a[x] = i; // 标记所属的组别 } } while (1) { cin >> opt; if (opt == "STOP") return 0; if (opt == "ENQUEUE") { // 入队操作 int x; cin >> x; int t = a[x]; if (q[t].size() == 0) qq.push(t); q[t].push(x); } else { // 出队操作 int t = qq.front(); cout << q[t].front() << endl; q[t].pop(); if (q[t].size() == 0) qq.pop(); } } return 0; } // RE 数组越界,除数 为 0 队列为空,弹出,RE ,STL 队列 #include<bits/stdc++.h> using namespace std; const int N = 1e3 + 10; int a[N], n, m, b[N]; queue q; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = a[i]; } m++; sort(b + 1, b + 1 + n, greater()); for (int i = 1; i <= n; i++) q.push(i); // 存储的是下标 for (int i = 1; i <= n; i++) { // 先保证 队列里面最开的值和要出队列的值相等 while (a[q.front()] != b[i]) { q.push(q.front()); q.pop(); } // a[q.front()] == b[i] 成立 if (q.front() == m) { cout << i; return 0; } q.pop(); } return 0; } //stack stk; //stk.push(x); //入栈 //stk.pop(); //出栈 //stk.size(); // 栈的长度 //stk.top(); // 栈顶元素 #include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10;

    int n, T; unsigned long long x; string opt; int main() { cin >> T; while (T--) { stack stk; cin >> n; while (n--) { cin >> opt; if (opt == "push") { cin >> x; stk.push(x); } else if (opt == "pop") { if (stk.size()) stk.pop(); else puts("Empty"); } else if (opt == "query") { if (stk.size()) cout << stk.top() << endl; else puts("Anguei!"); } else cout << stk.size() << endl; } } return 0; } //int stk[N]; //int top; // 栈顶 //入栈: stk[++top] =x; //出栈 : --top; //栈的元素个数: top //if(top) 栈不为空 #include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10; unsigned long long stk[N]; int n, T, top; unsigned long long x; string opt; int main() { cin >> T; while (T--) { top = 0; //; 多组测试数据,一定要清空 cin >> n; while (n--) { cin >> opt; if (opt == "push") { cin >> x; stk[++top] = x; } else if (opt == "pop") { if (top) --top; else puts("Empty"); } else if (opt == "query") { if (top) cout << stk[top] << endl; else puts("Anguei!"); } else cout << top << endl; } } return 0; } //queue q; 生成一个队列 //q.push(x); // 入队 //q.pop(); // 出队 //q.front(); // 队首元素 //q.size(); // 队列长度 //q.empty() ; // 判断是否为空,为空值为1 #include<bits/stdc++.h> using namespace std; queue q; int n, opt, x; int main() { cin >> n; while (n--) { cin >> opt; if (opt == 1) { // 队列入队 cin >> x; q.push(x); } else if (opt == 2) { // 出队操作 if (q.size()) q.pop(); else puts("ERR_CANNOT_POP"); // 输出字符串,自带换行 } else if (opt == 3) { // 输出队首元素 if (q.size()) cout << q.front() << endl; else puts("ERR_CANNOT_QUERY"); } else cout << q.size() << endl; } return 0; }

    // 队列:先进先出 // 数组模拟队列 //head =0 , tail =-1 //入队: q[++tail] = x; //出队: head++; //队列长度: tail - head+1 //队列是否为空: head<=tail 成立,队列不为空 #include<bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int n, q[N], head = 0, tail = -1; int opt, x; int main() { cin >> n; while (n--) { cin >> opt; if (opt == 1) { // 队列入队 cin >> x; q[++tail] = x; } else if (opt == 2) { // 出队操作 if (head <= tail) ++head; else puts("ERR_CANNOT_POP"); // 输出字符串,自带换行 } else if (opt == 3) { // 输出队首元素 if (head <= tail) cout << q[head] << endl; else puts("ERR_CANNOT_QUERY"); } else cout << tail - head + 1 << endl; } return 0; }

    信息

    ID
    4487
    时间
    1000ms
    内存
    512MiB
    难度
    8
    标签
    (无)
    递交数
    700
    已通过
    105
    上传者