精讲 wording(比赛必备)
2026-05-20 20:44:13
发布于:广东
本文章面向初学者,旨在教会初学者最现代化的语法
1.STL Container
· Vector动态数组
好处:
1 用多少开多大数组,空间复杂度小
2 配合匿名函数,不需要在全局定义任何变量
3 每次定义都是初始化,不用担心忘记初始化
4 可以使用范围循环
5 可以动态变更长度
6 有 insert,erase 等方法(复杂度 O(n))
·Set
好处:
set 集合,可以 O(log)复杂度快速判断一个数字是否出现过
·Map
好处:
map 键值对映射,可以快速知道一个 key 对应的是什么 val
queue、stack、deque、priority_queue等,我就不在讲了(后面学)
样列1(one-dimensional vector)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);//不需要开全局
for(auto &x : a) cin >> x;//匿名函数输入(仅用于1维度)
} //该代码可以快速输入一个长度为n的数组
样列2(two-dimensional vector)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
// 定义二维vector
vector<vector<int>> v(n, vector<int>(m));
// 循环输入
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> v[i][j];
return 0;
}//该代码可以快速输入一个n,m的二维数组
以上是关于STL container 的主要讲解
2.打开输入、输出封印(快速输入输出)
代码:
cin.tie(0)->sync_with_stdio(0);
cout << '\n';
为什么要用这个
因为很快:
1.打开封印 1.41s
2.普通 cin 2.35s
3.'\n' 代替 endl 422ms
4.scanf 594ms
注意:
1. 不能和 c 风格的输入输出函数混用 scanf, printf, putchar, puts
2. 结果会一次性输出
3.范围循环(匿名函数)
代码
int a[10];
for (auto &x: a) cin >> x;
for (auto x : a) cout << x << '\n';
非常高级也非常实用,建议写一维题用它
列题( P1838 三子棋I)
代码1(普通)
#include <bits/stdc++.h>
using namespace std;
int board[3][3];
bool same(int x, int a, int b, int c) {
return a == x && b == x && c == x;
}
bool win(int x) {
for (int i = 0; i < 3; i++) {
if (same(x, board[i][0], board[i][1], board[i][2])) return true;
if (same(x, board[0][i], board[1][i], board[2][i])) return true;
}
if (same(x, board[0][0], board[1][1], board[2][2])) return true;
if (same(x, board[2][0], board[1][1], board[0][2])) return true;
return false;
}
int main() {
string str;
cin >> str;
for (int i = 0; i < str.size(); i++) {
int pos = str[i] - '1';
int x = pos / 3, y = pos % 3;
board[x][y] = 1 + i % 2;
}
if (win(1)) cout << "xiaoa wins." << endl;
else if (win(2)) cout << "uim wins." << endl;
else cout << "drew." << endl;
}
代码2(匿名函数)
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
int board[3][3];
for (size_t i = 0; i < str.size(); i++) {
int pos = str[i] - '1';
int x = pos / 3, y = pos % 3;
board[x][y] = 1 + i % 2;
}
auto win = [&](int x) {
auto same = [&](int a, int b, int c) {
return a == x && b == x && c == x;
};
for (int i = 0; i < 3; i++) {
if (same(board[i][0], board[i][1], board[i][2])) return true;
if (same(board[0][i], board[1][i], board[2][i])) return true;
}
if (same(board[0][0], board[1][1], board[2][2])) return true;
if (same(board[2][0], board[1][1], board[0][2])) return true;
return false;
};
if (win(1)) cout << "xiaoa wins." << endl;
else if (win(2)) cout << "uim wins." << endl;
else cout << "drew." << endl;
}
本篇文章到这就没了,希望大家以后能用更现代化的语法来做题
全部评论 2
收到
昨天 来自 广东
0但是太现代了,又丑又难看懂
昨天 来自 广东
0这是现在IOI都用的写法
昨天 来自 广东
0
is good

昨天 来自 天津
06
昨天 来自 广东
0























有帮助,赞一个