CSP-J 大纲
CSP-J(入门级)大纲涵盖编程语言、数据结构、算法、数学基础等核心领域。以下是2025年最新大纲知识点的详细解析,结合数学公式与算法原理:
一、编程语言基础(C++)
1. 数据类型与运算符
- 基本类型:整型(
int、long long)、浮点型(double)、字符型(char)、布尔型(bool)。 - 运算符:
- 算术运算符:
+、-、*、/、%(取余)。 - 逻辑运算符:
&&(与)、||(或)、!(非)。 - 位运算符:
&(按位与)、|(按位或)、^(异或)、<<(左移)、>>(右移)。
- 算术运算符:
- 表达式求值:遵循运算符优先级与结合性,如
a + b * c先乘后加。
2. 控制结构
- 条件语句:
if-else、switch-case。 - 循环语句:
for循环:for (初始化; 条件; 更新)。while循环:while (条件)。do-while循环:do { ... } while (条件)。
- 跳转语句:
break(跳出循环)、continue(跳过本次循环)。
3. 函数与数组
- 函数定义:
返回类型 函数名(参数列表) { 函数体 }。 - 数组:
- 一维数组:
int a[n];,访问a[i]。 - 二维数组:
int a[m][n];,访问a[i][j]。 - 字符串:
string s;,支持length()、substr()等方法。
- 一维数组:
4. 指针与引用(新增)
- 指针:存储变量地址,如
int *p = &a;。 - 引用:给变量取别名,
int &ref = a;,用于函数参数传递时直接修改原值。
二、数据结构
1. 线性结构
- 链表:由节点(数据+指针)组成,支持动态插入/删除。
- 栈:后进先出(LIFO),操作
push()、pop()、top()。 - 队列:先进先出(FIFO),操作
enqueue()、dequeue()、front()。
2. 树结构
- 二叉树:每个节点最多两个子节点。
- 遍历方式:
- 前序遍历:根→左→右。
- 中序遍历:左→根→右。
- 后序遍历:左→右→根。
- 层序遍历:逐层访问。
- 完全二叉树:除最后一层外,其他层节点数满,且最后一层节点左对齐。
- 遍历方式:
- 哈夫曼树:带权路径长度最小的二叉树,用于数据压缩。
- 二叉搜索树:左子树节点值<根节点值<右子树节点值。
3. 图结构
- 存储方式:
- 邻接矩阵:用二维数组表示边权,空间复杂度
O(V^2) 。 - 邻接表:用链表数组表示,空间复杂度
O(V+E) 。
- 邻接矩阵:用二维数组表示边权,空间复杂度
- 遍历算法:
- 深度优先搜索(DFS):递归或栈实现。
- 广度优先搜索(BFS):队列实现。
- 泛洪算法(Flood Fill):用于图像连通区域填充。
三、算法基础
1. 排序算法
- 冒泡排序:相邻元素比较交换,时间复杂度
O(n^2) 。 - 选择排序:每次选最小/大元素,时间复杂度
O(n^2) 。 - 插入排序:逐个插入已排序序列,时间复杂度
O(n^2) 。 - 计数排序:统计元素出现次数,时间复杂度
O(n+k) (k为数据范围)。
2. 搜索算法
- 二分查找:在有序数组中查找,时间复杂度
O(\log n) 。- 公式:
low = 0; high = n-1; while (low <= high) { mid = (low + high) / 2; ... }。
- 公式:
- 深度优先搜索(DFS):用于遍历图或树,递归实现。
- 广度优先搜索(BFS):用于求最短路径,队列实现。
3. 贪心算法
- 核心思想:每一步选择当前最优解,如活动选择问题。
- 应用场景:
- 背包问题(物品不可分割):选价值密度最大的物品。
- 区间调度:按结束时间排序,选不重叠区间。
4. 动态规划(DP)
- 基本思路:将问题分解为子问题,存储子问题解避免重复计算。
- 类型:
- 一维DP:如斐波那契数列
f(n) = f(n-1) + f(n-2) 。 - 背包DP:0-1背包问题,状态转移方程
dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]) 。 - 区间DP:如矩阵连乘,状态转移方程
dp[i][j] = min(dp[i][k] + dp[k+1][j] + cost[i][j]) 。 - 树型DP(新增):如求二叉树最大路径和,状态定义为以当前节点为根的子树最大值。
- 一维DP:如斐波那契数列
5. 前缀和与差分(新增)
- 前缀和:数组
S满足S[i] = S[i-1] + a[i] ,快速求区间和S[r] - S[l-1] 。 - 差分:数组
d满足d[i] = a[i] - a[i-1] ,快速实现区间加减操作。
四、数学基础
1. 数论
- 模运算:
a \mod b 表示a除以b的余数,性质: -
- 欧几里得算法:求最大公约数
\gcd(a, b) = \gcd(b, a \mod b) 。 - 素数筛法:
- 埃氏筛法:时间复杂度
O(n \log \log n) 。 - 线性筛法:时间复杂度
O(n) 。
- 埃氏筛法:时间复杂度
- 欧拉函数(新增):
\phi(n) = n \prod_{p|n} \left(1 - \frac{1}{p}\right) ,表示1~n中与n互质的数的个数。
2. 组合数学
- 排列组合:
- 排列数:
P(n, k) = \frac{n!}{(n-k)!} 。 - 组合数:
C(n, k) = \frac{n!}{k!(n-k)!} 。
- 排列数:
- 二项式定理(新增):
(a + b)^n = \sum_{k=0}^n \binom{n}{k} a^{n-k} b^k 。 - 容斥原理:三个集合的并集大小
|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| 。
3. 递推关系
- 斐波那契数列:
f(n) = f(n-1) + f(n-2) ,初始条件f(0)=0 ,f(1)=1 。 - 错位排列:
D(n) = (n-1)(D(n-1) + D(n-2)) 。
五、计算机系统基础
1. 操作系统
- 进程与线程:进程是资源分配单位,线程是执行单位。
- 内存管理:虚拟内存、分页机制。
- 文件系统:文件的存储、访问与管理。
2. 网络基础
- TCP/IP协议:包括IP(网络层)、TCP(传输层)、HTTP(应用层)。
- 网络模型:OSI七层模型与TCP/IP四层模型。
3. 编译原理
- 编译过程:预处理、编译、汇编、链接。
- 常见错误:语法错误、运行时错误、逻辑错误。
六、其他知识点
1. 高精度运算
- 加法:逐位相加,处理进位。
- 减法:逐位相减,处理借位。
- 乘法:模拟竖式乘法,时间复杂度
O(n^2) 。 - 除法:高精度整数除以单精度整数,得到商和余数。
2. 位运算
- 按位操作:
&(与)、|(或)、^(异或)、~(取反)。 - 应用:判断奇偶(
n & 1)、取二进制位(n >> k & 1)。
3. 复杂度分析
- 时间复杂度:
- 常数阶:
O(1) ,如数组访问。 - 线性阶:
O(n) ,如遍历数组。 - 对数阶:
O(\log n) ,如二分查找。 - 平方阶:
O(n^2) ,如冒泡排序。
- 常数阶:
- 空间复杂度:算法所需额外空间,如递归栈空间。
七、新增知识点解析
1. 引用(指针部分)
- 作用:替代指针简化代码,避免指针空悬问题。
- 示例:
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
2. 前缀和与差分
- 前缀和:
int S[n+1]; S[0] = 0; for (int i=1; i<=n; i++) S[i] = S[i-1] + a[i];区间和
S[r] - S[l-1] 。 - 差分:
int d[n+2]; // 辅助数组 d[l] += k; d[r+1] -= k;区间加操作后,通过前缀和恢复原数组。
3. 欧拉函数
- 计算示例:
-
- 1~6中与6互质的数:1, 5。
-
Code
以下是CSP-J大纲核心知识点的标准代码实现,覆盖编程基础、数据结构、算法及数学相关内容,代码采用C++语言并遵循竞赛规范:
一、编程语言基础
1. 数据类型与运算符
#include <iostream>
using namespace std;
int main() {
// 基本数据类型
int a = 10;
long long b = 1e18;
double c = 3.14159;
char d = 'A';
bool e = true;
// 算术运算符
int sum = a + 5;
int mod = a % 3; // 10 % 3 = 1
// 位运算符
int bit_and = a & 3; // 1010 & 0011 = 0010 (2)
int bit_or = a | 3; // 1010 | 0011 = 1011 (11)
int left_shift = a << 1; // 1010 << 1 = 10100 (20)
cout << "sum: " << sum << ", mod: " << mod << endl;
return 0;
}
2. 控制结构
#include <iostream>
using namespace std;
int main() {
// if-else条件语句
int x = 5;
if (x > 0) {
cout << "Positive" << endl;
} else if (x < 0) {
cout << "Negative" << endl;
} else {
cout << "Zero" << endl;
}
// for循环
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
cout << endl;
// while循环
int j = 0;
while (j < 5) {
cout << j << " ";
j++;
}
cout << endl;
// switch-case
int day = 3;
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
default: cout << "Other";
}
return 0;
}
3. 函数与数组
#include <iostream>
#include <string>
using namespace std;
// 函数定义:求数组和
int arraySum(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main() {
// 一维数组
int nums[5] = {1, 2, 3, 4, 5};
cout << "Sum: " << arraySum(nums, 5) << endl;
// 二维数组
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
// 字符串操作
string s = "Hello";
s += " World";
cout << s << ", length: " << s.length() << endl;
cout << s.substr(0, 5) << endl; // 截取子串
return 0;
}
4. 指针与引用
#include <iostream>
using namespace std;
// 引用作为参数(实现两数交换)
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
// 指针基础
int x = 10;
int *p = &x; // 指针指向x的地址
cout << "x = " << x << ", *p = " << *p << endl;
*p = 20; // 通过指针修改x的值
cout << "x = " << x << endl;
// 引用示例
int a = 5, b = 10;
swap(a, b);
cout << "a = " << a << ", b = " << b << endl; // 输出10,5
return 0;
}
二、数据结构
1. 线性结构
- 链表
#include <iostream> using namespace std;
// 链表节点定义 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} };
// 链表插入(头部插入) void insertHead(ListNode &head, int val) { ListNode newNode = new ListNode(val); newNode->next = head; head = newNode; }
// 链表遍历 void printList(ListNode *head) { while (head != nullptr) { cout << head->val << " -> "; head = head->next; } cout << "nullptr" << endl; }
int main() { ListNode *head = nullptr; insertHead(head, 3); insertHead(head, 2); insertHead(head, 1); printList(head); // 1 -> 2 -> 3 -> nullptr return 0; }
- **栈与队列(STL实现)**
```cpp
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main() {
// 栈(LIFO)
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
while (!st.empty()) {
cout << st.top() << " "; // 3 2 1
st.pop();
}
cout << endl;
// 队列(FIFO)
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
while (!q.empty()) {
cout << q.front() << " "; // 1 2 3
q.pop();
}
cout << endl;
return 0;
}
2. 树结构(二叉树遍历)
#include <iostream>
using namespace std;
// 二叉树节点定义
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 前序遍历:根->左->右
void preorder(TreeNode *root) {
if (root == nullptr) return;
cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}
// 中序遍历:左->根->右
void inorder(TreeNode *root) {
if (root == nullptr) return;
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}
// 后序遍历:左->右->根
void postorder(TreeNode *root) {
if (root == nullptr) return;
postorder(root->left);
postorder(root->right);
cout << root->val << " ";
}
int main() {
// 构建示例二叉树
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
cout << "Preorder: "; preorder(root); cout << endl; // 1 2 4 5 3
cout << "Inorder: "; inorder(root); cout << endl; // 4 2 5 1 3
cout << "Postorder: "; postorder(root); cout << endl;// 4 5 2 3 1
return 0;
}
3. 图结构(邻接表与DFS/BFS)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 邻接表存储图
vector<vector<int>> adj; // adj[u]表示u的邻接节点
vector<bool> visited;
// 深度优先搜索
void dfs(int u) {
visited[u] = true;
cout << u << " ";
for (int v : adj[u]) {
if (!visited[v]) {
dfs(v);
}
}
}
// 广度优先搜索
void bfs(int start) {
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << " ";
for (int v : adj[u]) {
if (!visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
}
int main() {
int n = 5; // 节点数:0~4
adj.resize(n);
// 添加边
adj[0].push_back(1);
adj[0].push_back(2);
adj[1].push_back(3);
adj[1].push_back(4);
adj[2].push_back(4);
// DFS
visited.assign(n, false);
cout << "DFS: ";
dfs(0); // 0 1 3 4 2
cout << endl;
// BFS
visited.assign(n, false);
cout << "BFS: ";
bfs(0); // 0 1 2 3 4
cout << endl;
return 0;
}
三、算法基础
1. 排序算法
-
冒泡排序
void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { swap(arr[j], arr[j+1]); // 交换 } } } } -
快速排序(简化版)
void quickSort(int arr[], int left, int right) { if (left >= right) return; int pivot = arr[left]; int i = left, j = right; while (i < j) { while (i < j && arr[j] >= pivot) j--; arr[i] = arr[j]; while (i < j && arr[i] <= pivot) i++; arr[j] = arr[i]; } arr[i] = pivot; quickSort(arr, left, i-1); quickSort(arr, i+1, right); }
2. 二分查找
#include <iostream>
using namespace std;
// 在有序数组中查找target,返回索引(-1表示不存在)
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2; // 避免溢出
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {1, 3, 5, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
cout << binarySearch(arr, n, 5) << endl; // 2
cout << binarySearch(arr, n, 4) << endl; // -1
return 0;
}
3. 动态规划(0-1背包问题)
#include <iostream>
#include <vector>
using namespace std;
// 0-1背包:n个物品,重量w[],价值v[],背包容量cap,求最大价值
int knapsack(int n, int cap, int w[], int v[]) {
vector<vector<int>> dp(n+1, vector<int>(cap+1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= cap; j++) {
if (j >= w[i-1]) { // 第i个物品的索引是i-1
dp[i][j] = max(dp[i-1][j], dp[i-1][j - w[i-1]] + v[i-1]);
} else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n][cap];
}
int main() {
int w[] = {2, 3, 4};
int v[] = {3, 4, 5};
int cap = 5;
cout << knapsack(3, cap, w, v) << endl; // 7(选前两个物品)
return 0;
}
4. 前缀和与差分
#include <iostream>
#include <vector>
using namespace std;
// 前缀和:求区间[l, r]的和(1-based)
int prefixSum(const vector<int>& a, int l, int r) {
int n = a.size();
vector<int> s(n+1, 0);
for (int i = 1; i <= n; i++) {
s[i] = s[i-1] + a[i-1];
}
return s[r] - s[l-1];
}
// 差分:区间[l, r]加k(1-based)
void rangeAdd(vector<int>& a, int l, int r, int k) {
int n = a.size();
vector<int> d(n+2, 0); // 大小n+2避免越界
d[l] += k;
d[r+1] -= k;
// 恢复原数组
for (int i = 1; i <= n; i++) {
d[i] += d[i-1];
a[i-1] = d[i];
}
}
int main() {
vector<int> a = {1, 2, 3, 4, 5};
cout << "区间[2,4]的和:" << prefixSum(a, 2, 4) << endl; // 2+3+4=9
rangeAdd(a, 2, 4, 10); // 区间[2,4]各元素加10
for (int x : a) {
cout << x << " "; // 1 12 13 14 5
}
return 0;
}
四、数学基础
1. 欧几里得算法(求最大公约数)
#include <iostream>
using namespace std;
// 递归实现gcd
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
// 扩展:求最小公倍数lcm(a,b) = a*b / gcd(a,b)
int lcm(int a, int b) {
return a / gcd(a, b) * b; // 先除后乘避免溢出
}
int main() {
cout << gcd(12, 18) << endl; // 6
cout << lcm(12, 18) << endl; // 36
return 0;
}
2. 素数筛法(埃氏筛)
#include <iostream>
#include <vector>
using namespace std;
// 埃氏筛:求1~n中的所有素数
vector<int> sieve(int n) {
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
vector<int> primes;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) primes.push_back(i);
}
return primes;
}
int main() {
vector<int> primes = sieve(30);
for (int p : primes) {
cout << p << " "; // 2 3 5 7 11 13 17 19 23 29
}
return 0;
}
3. 高精度加法
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 高精度加法:a + b(a和b是逆序存储的数字,如"123"存储为[3,2,1])
vector<int> add(vector<int>& a, vector<int>& b) {
vector<int> res;
int carry = 0;
int i = 0;
while (i < a.size() || i < b.size() || carry) {
int sum = carry;
if (i < a.size()) sum += a[i];
if (i < b.size()) sum += b[i];
res.push_back(sum % 10);
carry = sum / 10;
i++;
}
return res;
}
int main() {
string sa = "123456", sb = "7890";
vector<int> a, b;
// 字符串转逆序数字数组
for (int i = sa.size()-1; i >= 0; i--) a.push_back(sa[i] - '0');
for (int i = sb.size()-1; i >= 0; i--) b.push_back(sb[i] - '0');
vector<int> c = add(a, b);
reverse(c.begin(), c.end()); // 逆序后输出
for (int x : c) {
cout << x; // 123456 + 7890 = 131346
}
return 0;
}