作业介绍
//3.5.2.-*7.+@
//16
//10.28.30./*7.-@
//-7
// 遇到数字入栈, 遇到符号,取两个元素,
// 计算结果后入栈
#include<bits/stdc++.h>
using namespace std;
const int N = 60;
int stk[N], top;
string s;
int main() {
cin >> s;
int num = 0;
for (char c : s) { // 依次处理单个字符
if (c == '+') {
int a = stk[top--], b = stk[top--];
stk[++top] = a + b;
} else if (c == '-') {
int a = stk[top--], b = stk[top--];
stk[++top] = b - a;
} else if (c == '*') {
int a = stk[top--], b = stk[top--];
stk[++top] = b * a;
} else if (c == '/') {
int a = stk[top--], b = stk[top--];
stk[++top] = b / a;
} else if (c == '.') { // 证明之前肯定是数字
stk[++top] = num;
num = 0;
} else { // 数字字符
// 123456
num = num * 10 + c - '0';
}
}
cout << stk[top];
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int e[N], nxt[N], tot;
int head = 1;
int a[N];
void insert(int x, int y) {
++tot;
e[tot] = y;
nxt[tot] = nxt[a[x]];
nxt[a[x]] = tot;
a[y] = tot;
}
void dele(int x) {
int t = a[x];
nxt[t] = nxt[nxt[t]];
}
int n, opt, x, y;
int main() {
insert(0, 1);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> opt >> x;
if (opt == 1) {
cin >> y;
insert(x, y);
} else if (opt == 2) {
cout << e[nxt[a[x]]] << endl;
}else{
dele(x);
}
}
return 0;
}
#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;
queue<int>q[N]; // 给每一个组别开一个队列
queue<int> 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<int> 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<int>());
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<int> 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<unsigned long long > 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<int> q; 生成一个队列
//q.push(x); // 入队
//q.pop(); // 出队
//q.front(); // 队首元素
//q.size(); // 队列长度
//q.empty() ; // 判断是否为空,为空值为1
#include<bits/stdc++.h>
using namespace std;
queue<int> 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;
}
题目
认领作业后才可以查看作业内容。
- 状态
- 正在进行…
- 题目
- 22
- 开始时间
- 2026-8-1 0:00
- 截止时间
- 2026-9-30 23:59
- 可延期
- 24 小时