CSP-J需要掌握的算法就这么简单?

栗子爱摸题

  • CSP-J可能涉及的算法及解决方案
    • 1. 排序算法
      • 冒泡排序 (Bubble Sort)
      • 插入排序 (Insertion Sort)
      • 选择排序 (Selection Sort)
      • 快速排序 (Quick Sort)
      • 归并排序 (Merge Sort)
    • 2. 查找算法
      • 二分查找 (Binary Search)
    • 3. 图算法
      • 广度优先搜索 (BFS)
      • 深度优先搜索 (DFS)
    • 4. 动态规划
    • 5. 贪心算法
    • 6. 回溯算法

CSP-J可能涉及的算法及解决方案

1. 排序算法

冒泡排序 (Bubble Sort)

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

插入排序 (Insertion Sort)

void insertionSort(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i-1;
        while (j >= 0 && arr[j] > key) {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = key;
    }
}

选择排序 (Selection Sort)

void selectionSort(int arr[], int n) {
    for (int i = 0; i < n-1; i++) {
        int minIndex = i;
        for (int j = i+1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        swap(arr[minIndex], arr[i]);
    }
}

快速排序 (Quick Sort)

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j <= high-1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i+1], arr[high]);
    return i+1;
}

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi-1);
        quickSort(arr, pi+1, high);
    }
}

归并排序 (Merge Sort)

void merge(int arr[], int l, int m, int r) {
    int n1 = m - l + 1;
    int n2 = r - m;

    int L[n1], R[n2];
    for (int i = 0; i < n1; i++) {
        L[i] = arr[l + i];
    }
    for (int j = 0; j < n2; j++) {
        R[j] = arr[m + 1 + j];
    }

    int i = 0;
    int j = 0;
    int k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l + (r - l) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
        merge(arr, l, m, r);
    }
}

2. 查找算法

二分查找 (Binary Search)

int binarySearch(int arr[], int low, int high, int target) {
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) {
            return mid;
        }
        if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return -1;
}

3. 图算法

广度优先搜索 (BFS)

void bfs(vector<vector<int>>& graph, int start) {
    int n = graph.size();
    vector<bool> visited(n, false);
    queue<int> q;
    visited[start] = true;
    q.push(start);
    while (!q.empty()) {
        int curr = q.front();
        q.pop();
        cout << curr << " ";
        for (int i = 0; i < graph[curr].size(); i++) {
            int neighbor = graph[curr][i];
            if (!visited[neighbor]) {
                visited[neighbor] = true;
                q.push(neighbor);
            }
        }
    }
}

深度优先搜索 (DFS)

void dfs(vector<vector<int>>& graph, int start, vector<bool>& visited) {
    visited[start] = true;
    cout << start << " ";
    for (int i = 0; i < graph[start].size(); i++) {
        int neighbor = graph[start][i];
        if (!visited[neighbor]) {
            dfs(graph, neighbor, visited);
        }
    }
}

void dfs(vector<vector<int>>& graph, int start) {
    int n = graph.size();
    vector<bool> visited(n, false);
    dfs(graph, start, visited);
}

当然可以!下面是一些典型的例题,涵盖了动态规划、贪心算法和回溯算法等常见算法问题。

4. 动态规划

问题描述:给定一个整数数组,找到一个连续子数组,使得子数组的和最大。

int maxSubarraySum(vector<int>& nums) {
    int n = nums.size();
    if (n == 0) {
        return 0;
    }
    int maxSum = nums[0];
    int currSum = nums[0];
    for (int i = 1; i < n; i++) {
        currSum = max(nums[i], currSum + nums[i]);
        maxSum = max(maxSum, currSum);
    }
    return maxSum;
}

5. 贪心算法

问题描述:给定一个区间列表,找到可以安排的最大活动数量,使得它们不会相互冲突。

struct Activity {
    int start;
    int end;
};

bool compare(Activity a, Activity b) {
    return a.end < b.end;
}

int maxActivities(vector<Activity>& activities) {
    sort(activities.begin(), activities.end(), compare);
    int n = activities.size();
    int maxCount = 1;
    int prevEnd = activities[0].end;
    for (int i = 1; i < n; i++) {
        if (activities[i].start >= prevEnd) {
            maxCount++;
            prevEnd = activities[i].end;
        }
    }
    return maxCount;
}

6. 回溯算法

问题描述:给定一个集合,找到所有可能的子集。

void backtrack(vector<int>& nums, int start, vector<int>& subset, vector<vector<int>>& result) {
    result.push_back(subset);
    for (int i = start; i < nums.size(); i++) {
        subset.push_back(nums[i]);
        backtrack(nums, i + 1, subset, result);
        subset.pop_back();
    }
}

vector<vector<int>> subsets(vector<int>& nums) {
    vector<vector<int>> result;
    vector<int> subset;
    backtrack(nums, 0, subset, result);
    return result;
}

你可能感兴趣的:(算法)