CSP-J 大纲

· · 个人记录

CSP-J(入门级)大纲涵盖编程语言、数据结构、算法、数学基础等核心领域。以下是2025年最新大纲知识点的详细解析,结合数学公式与算法原理:

一、编程语言基础(C++)

1. 数据类型与运算符

2. 控制结构

3. 函数与数组

4. 指针与引用(新增)

二、数据结构

1. 线性结构

2. 树结构

3. 图结构

三、算法基础

1. 排序算法

2. 搜索算法

3. 贪心算法

4. 动态规划(DP)

5. 前缀和与差分(新增)

四、数学基础

1. 数论

2. 组合数学

3. 递推关系

五、计算机系统基础

1. 操作系统

2. 网络基础

3. 编译原理

六、其他知识点

1. 高精度运算

2. 位运算

3. 复杂度分析

七、新增知识点解析

1. 引用(指针部分)

2. 前缀和与差分

3. 欧拉函数

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. 线性结构

// 链表节点定义 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. 排序算法

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;
}