#16523. CSP-J初赛完善程序题合集(2020-2025)

CSP-J初赛完善程序题合集(2020-2025)

CSP-J初赛完善程序题合集(2020-2025)

包含 60 道题,覆盖2020年(10题)、2021年(10题)、2022年(10题)、2023年(10题)、2024年(10题)、2025年(10题)。 题目按年份升序排列,编号已重新编排。


2020年

1 #include <cstdio>
2 using namespace std;
3 
4 int n, i;
5 
6 int main() {
7     scanf("%d", &n);
8     for (i = ①; ② <= n; i++) {
9         ③ {
10            printf("%d ", i);
11            n = n / i;
12        }
13    }
14    if (④) {
15        printf("%d ", ⑤);
16    }
17    return 0;
18 }
  1. ① 处应填 {{ select(1) }}
  • 1
  • n−1
  • 2
  • 0
  1. ② 处应填 {{ select(2) }}
  • n / i
  • n / (i * i)
  • i * i
  • i
  1. ③ 处应填 {{ select(3) }}
  • if (n % i == 0)
  • while (n % i == 0)
  • if (n / i == 0)
  • while (n / i == 0)
  1. ④ 处应填 {{ select(4) }}
  • n > 1
  • n == 1
  • i > 1
  • i == 0
  1. ⑤ 处应填( ) {{ select(5) }}
  • i
  • n
  • 1
  • 0
1 #include <iostream>
2 using namespace std;
3 
4 const int MAXN = 5000;
5 int n, m;
6 struct segment { int a, b; } A[MAXN];
7 
8 void sort() // 排序
9 {
10    for (int i = 0; i < n; i++)
11        for (int j = 1; j < n; j++)
12            if (①) {
13                segment t = A[j];
14                ②
15            }
16 }
17 
18 int main() {
19    cin >> n >> m;
20    for (int i = 0; i < n; i++)
21        cin >> A[i].a >> A[i].b;
22    sort();
23    int p = 1;
24    for (int i = 1; i < n; i++)
25        if (③)
26            A[p++] = A[i];
27    n = p;
28    int ans = 0, r = 0;
29    int q = 0;
30    while (r < m) {
31        while (④)
32            q++;
33        ⑤;
34        ans++;
35    }
36    cout << ans << endl;
37    return 0;
38 }
  1. ① 处应填 {{ select(6) }}
  • A[j].b > A[j-1].b
  • A[j].a < A[j-1].a
  • A[j].a > A[j-1].a
  • A[j].b < A[j-1].b
  1. ② 处应填 {{ select(7) }}
  • A[j+1] = A[j]; A[j] = t;
  • A[j-1] = A[j]; A[j] = t;
  • A[j] = A[j+1]; A[j+1] = t;
  • A[j] = A[j-1]; A[j-1] = t;
  1. ③ 处应填 {{ select(8) }}
  • A[i].b > A[p-1].b
  • A[i].b < A[i-1].b
  • A[i].b > A[i-1].b
  • A[i].b < A[p-1].b
  1. ④ 处应填 {{ select(9) }}
  • q+1 < n && A[q+1].a <= r
  • q+1 < n && A[q+1].b <= r
  • q < n && A[q].a <= r
  • q < n && A[q].b <= r
  1. ⑤ 处应填( ) ### 2020 年 {{ select(10) }}
  • q++
  • r = max(r, A[q+1].b)
  • r = max(r, A[q].b)
  • r = max(r, A[q+1].a)

2021年

1 #include <iostream>
2 using namespace std;
3 
4 const int MAXN = 1000000;
5 int F[MAXN];
6 
7 int main() {
8     int n;
9     cin >> n;
10    int i = 0, p = 0, c = 0;
11    while (①) {
12        if (F[i] == 0) {
13            if (②) {
14                F[i] = 1;
15                ③;
16            }
17            ④;
18        }
19        ⑤;
20    }
21    int ans = -1;
22    for (i = 0; i < n; i++)
23        if (F[i] == 0) ans = i;
24    cout << ans << endl;
25    return 0;
26 }
  1. ① 处应填 {{ select(11) }}
  • i < n
  • c < n
  • i < n - 1
  • c < n - 1
  1. ② 处应填 {{ select(12) }}
  • i % 2 == 0
  • i % 2 == 1
  • !p
  • p
  1. ③ 处应填 {{ select(13) }}
  • i++
  • i = (i + 1) % n
  • p ^= 1
  • c++
  1. ④ 处应填 {{ select(14) }}
  • p ^= 1
  • c++
  • i = (i + 1) % n
  • i++
  1. ⑤ 处应填( ) {{ select(15) }}
  • c++
  • i = (i + 1) % n
  • p ^= 1
  • i++
1 #include <iostream>
2 using namespace std;
3 
4 struct point {
5     int x, y, id;
6 };
7 
8 bool equals(point a, point b) {
9     return a.x == b.x && a.y == b.y;
10 }
11 
12 bool cmp(point a, point b) {
13     return ①;
14 }
15 
16 void sort(point A[], int n) {
17     for (int i = 0; i < n; i++)
18         for (int j = 1; j < n; j++)
19             if (cmp(A[j], A[j - 1])) {
20                 point t = A[j];
21                 A[j] = A[j - 1];
22                 A[j - 1] = t;
23             }
24 }
25 
26 int unique(point A[], int n) {
27     int t = 0;
28     for (int i = 0; i < n; i++)
29         if (②)
30             A[t++] = A[i];
31     return t;
32 }
33 
34 bool binary_search(point A[], int n, int x, int y) {
35     point p;
36     p.x = x; p.y = y; p.id = n;
37     int a = 0, b = n - 1;
38     while (a < b) {
39         int mid = ③;
40         if (④)
41             a = mid + 1;
42         else
43             b = mid;
44     }
45     return equals(A[a], p);
46 }
47 
48 const int MAXN = 1000;
49 point A[MAXN];
50 
51 int main() {
52     int n;
53     cin >> n;
54     for (int i = 0; i < n; i++) {
55         cin >> A[i].x >> A[i].y;
56         A[i].id = i;
57     }
58     sort(A, n);
59     n = unique(A, n);
60     int ans = 0;
61     for (int i = 0; i < n; i++)
62         for (int j = 0; j < n; j++)
63             if (⑤ && binary_search(A, n, A[i].x, A[j].y) && binary_search(A, n, A[j].x, A[i].y)) {
64                 ans++;
65             }
66     cout << ans << endl;
67     return 0;
68 }
  1. ① 处应填 {{ select(16) }}
  • a.x != b.x ? a.x < b.x : a.y < b.y
  • equals(a, b) ? a.id < b.id : (a.x != b.x ? a.x < b.x : a.y < b.y)
  • equals(a, b) ? a.id < b.id : a.x < b.x
  • a.x != b.x ? a.x < b.x : a.id < b.id
  1. ② 处应填 {{ select(17) }}
  • i == 0 || !cmp(A[i], A[i-1])
  • t == 0 || !equals(A[i], A[t-1])
  • i == 0 || cmp(A[i], A[i-1])
  • t == 0 || equals(A[i], A[t-1])
  1. ③ 处应填 {{ select(18) }}
  • a + (b - a + 1) / 2
  • (a + b + 1) >> 1
  • b - (b - a) / 2 + 1
  • (a + b) >> 1
  1. ④ 处应填 {{ select(19) }}
  • cmp(A[mid], p)
  • !cmp(p, A[mid])
  • cmp(p, A[mid])
  • !cmp(A[mid], p)
  1. ⑤ 处应填( ) ### 2021 年 {{ select(20) }}
  • A[i].x == A[j].x && A[i].id < A[j].id
  • A[i].x < A[j].x && A[i].y < A[j].y
  • A[i].id < A[j].id
  • A[i].x == A[j].x

2022年

1 #include <bits/stdc++.h>
2 using namespace std;
3 
4 int main() {
5     int n;
6     cin >> n;
7     vector<int> fac;
8     fac.reserve((int)ceil(sqrt(n)));
9 
10    int i;
11    for (i = 1; i * i < n; ++i) {
12        if (①) {
13            fac.push_back(i);
14        }
15    }
16 
17    for (int k = 0; k < fac.size(); ++k) {
18        cout << ② << " ";
19    }
20    if (③) {
21        cout << ④ << " ";
22    }
23    for (int k = fac.size() - 1; k >= 0; --k) {
24        cout << ⑤ << " ";
25    }
26    return 0;
27 }
  1. ① 处应填 {{ select(21) }}
  • n % i == 0
  • n % i == 1
  • n % (i - 1) == 0
  • n % (i - 1) == 1
  1. ② 处应填 {{ select(22) }}
  • n / fac[k]
  • fac[k]
  • fac[k] - 1
  • n / (fac[k] - 1)
  1. ③ 处应填 {{ select(23) }}
  • (i - 1) * (i - 1) == n
  • (i - 1) * i == n
  • i * i == n
  • i * (i - 1) == n
  1. ④ 处应填 {{ select(24) }}
  • n - i
  • n - i + 1
  • i - 1
  • i
  1. ⑤ 处应填( ) {{ select(25) }}
  • n / fac[k]
  • fac[k]
  • fac[k] - 1
  • n / (fac[k] - 1)
1 #include <bits/stdc++.h>
2 using namespace std;
3 
4 const int ROWS = 8;
5 const int COLS = 8;
6 
7 struct Point {
8     int r, c;
9     Point(int r, int c) : r(r), c(c) {}
10 };
11 
12 bool is_valid(char image[ROWS][COLS], Point pt,
13               int prev_color, int new_color) {
14     int r = pt.r;
15     int c = pt.c;
16     return (0 <= r && r < ROWS && 0 <= c && c < COLS &&
17             ① && image[r][c] != new_color);
18 }
19 
20 void flood_fill(char image[ROWS][COLS], Point cur, int new_color) {
21     queue<Point> queue;
22     queue.push(cur);
23     int prev_color = image[cur.r][cur.c];
24     ②;
25     while (!queue.empty()) {
26         Point pt = queue.front();
27         queue.pop();
28         Point points[4] = {③, Point(pt.r - 1, pt.c),
29                           Point(pt.r, pt.c + 1), Point(pt.r, pt.c - 1)};
30         for (auto p : points) {
31             if (is_valid(image, p, prev_color, new_color)) {
32                 ④;
33                 ⑤;
34             }
35         }
36     }
37 }
38 
39 int main() {
40     char image[ROWS][COLS] = {
41         {'g','g','g','g','g','g','g','g'},
42         {'g','g','g','g','g','g','r','r'},
43         {'g','r','r','g','g','r','g','g'},
44         {'g','b','b','b','b','r','g','r'},
45         {'g','g','g','b','b','r','g','r'},
            {'g','g','g','b','b','b','b','r'},
            {'g','g','g','g','g','b','g','g'},
            {'g','g','g','g','g','b','b','g'},
	    Point cur(4, 4);
  		char new_color = 'y';
      flood_fill(image, cur, new_color);
    	for (int r = 0; r < ROWS; r++) {
         for (int c = 0; c < COLS; c++) {
         		cout << image[r][c] << " ";
         }
         cout << endl;
     }
     // 输出:
     // g g g g g g g g
     // g g g g g g r r
     // g r r g g r g g
     // g y y y y r g r
     // g g g y y r g r
     // g g g y y y y r
     // g g g g g y g g
     // g g g g g y y g
     return 0;
}


  1. ① 处应填 {{ select(26) }}
  • image[r][c] == prev_color
  • image[r][c] != prev_color
  • image[r][c] == new_color
  • image[r][c] != new_color
  1. ② 处应填 {{ select(27) }}
  • image[cur.r][cur.c] = new_color;
  • prev_color = new_color;
  • new_color = prev_color;
  • image[cur.r][cur.c] = prev_color;
  1. ③ 处应填 {{ select(28) }}
  • Point(pt.r, pt.c)
  • Point(pt.r, pt.c + 1)
  • Point(pt.r + 1, pt.c)
  • Point(pt.r + 1, pt.c + 1)
  1. ④ 处应填 {{ select(29) }}
  • prev_color = image[p.r][p.c];
  • new_color = image[p.r][p.c];
  • image[p.r][p.c] = prev_color;
  • image[p.r][p.c] = new_color;
  1. ⑤ 处应填( ) ### 2022 年 {{ select(30) }}
  • queue.push(p);
  • queue.push(pt);
  • queue.push(cur);
  • queue.push(Point(ROWS,COLS));

2023年

1 #include <iostream>
2 #include <vector>
3 using namespace std;
4 
5 int find_missing(vector<int>& nums) {
6     int left = 0, right = nums.size() - 1;
7     while (left < right) {
8         int mid = left + (right - left) / 2;
9         if (nums[mid] == mid + ①) {
10            ②;
11        } else {
12            ③;
13        }
14    }
15    return ④;
16 }
17 
18 int main() {
19    int n;
20    cin >> n;
21    vector<int> nums(n);
22    for (int i = 0; i < n; i++) cin >> nums[i];
23    int missing_number = find_missing(nums);
24    if (missing_number == ⑤) {
25        cout << "Sequence is consecutive" << endl;
26    } else {
27        cout << "Missing number is " << missing_number << endl;
28    }
29    return 0;
30 }
  1. ① 处应该填 {{ select(31) }}
  • 1
  • nums[0]
  • right
  • left
  1. ② 处应该填 {{ select(32) }}
  • left = mid + 1
  • right = mid - 1
  • right = mid
  • left = mid
  1. ③ 处应该填 {{ select(33) }}
  • left = mid + 1
  • right = mid - 1
  • right = mid
  • left = mid
  1. ④ 处应该填 {{ select(34) }}
  • left + nums[0]
  • right + nums[0]
  • mid + nums[0]
  • right + 1
  1. ⑤ 处应该填( ) {{ select(35) }}
  • nums[0] + n
  • nums[0] + n - 1
  • nums[0] + n + 1
  • nums[n - 1]
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5 
6 int min(int x, int y, int z) {
7     return min(min(x, y), z);
8 }
9 
10 int edit_dist_dp(string str1, string str2) {
11    int m = str1.length();
12    int n = str2.length();
13    vector<vector<int>> dp(m + 1, vector<int>(n + 1));
14 
15    for (int i = 0; i <= m; i++) {
16        for (int j = 0; j <= n; j++) {
17            if (i == 0)
18                dp[i][j] = ①;
19            else if (j == 0)
20                dp[i][j] = ②;
21            else if (③)
22                dp[i][j] = ④;
23            else
24                dp[i][j] = 1 + min(dp[i][j - 1], dp[i - 1][j], ⑤);
25        }
26    }
27    return dp[m][n];
28 }
29 
30 int main() {
31    string str1, str2;
32    cin >> str1 >> str2;
33    cout << "Minimum number of operation:" << edit_dist_dp(str1, str2) << endl;
34    return 0;
35 }
  1. ① 处应填 {{ select(36) }}
  • j
  • i
  • m
  • n
  1. ② 处应填 {{ select(37) }}
  • j
  • i
  • m
  • n
  1. ③ 处应填 {{ select(38) }}
  • str1[i - 1] == str2[j - 1]
  • str1[i] == str2[j]
  • str1[i - 1] != str2[j - 1]
  • str1[i] != str2[j]
  1. ④ 处应填 {{ select(39) }}
  • dp[i - 1][j - 1] + 1
  • dp[i - 1][j - 1]
  • dp[i - 1][j]
  • dp[i][j - 1]
  1. ⑤ 处应填( ) ### 2023 年 {{ select(40) }}
  • dp[i][j] + 1
  • dp[i - 1][j - 1] + 1
  • dp[i - 1][j - 1]
  • dp[i][j]

2024年

#include<iostream>
#include<vector>
using namespace std;

bool isSquare(int num) {
    int i = _①_;
    int bound = _②_;
    for (; i <= bound; ++i) {
        if (_③_) {
            return _④_;
        }
    }
    return _⑤_;
}

int main() {
    int n;
    cin >> n;
    if (isSquare(n)) {
        cout << n << " is a Square number" << endl;
    } else {
        cout << n << " is not a Square number" << endl;
    }
}
  1. ①处应填 {{ select(41) }}
  • 1
  • 2
  • 3
  • 4
  1. ②处应填 {{ select(42) }}
  • (int)floor(sqrt(num)-1)
  • (int)floor(sqrt(num))
  • floor(sqrt(num/2))-1
  • floor(sqrt(num/2))
  1. ③处应填 {{ select(43) }}
  • num = 2 * i
  • num == 2 * i
  • num = i * i
  • num == i * i
  1. ④处应填 {{ select(44) }}
  • num = 2 * i
  • num == 2 * i
  • true
  • false
#include <bits/stdc++.h>
using namespace std;

void move(char src, char tgt) {
    cout << "从柱子" << src << "挪到柱子上" << tgt << endl;
}

void dfs(int i, char src, char tmp, char tgt) {
    if (i == _①_) {
        move(_②_);
        return;
    }
    dfs(i - 1, _③_);
    move(src, tgt);
    dfs(_⑤_, _④_);
}

int main() {
    int n;
    cin >> n;
    dfs(n, 'A', 'B', 'C');
}
  1. ⑤处应填( ) 汉诺塔问题 问题:给定三根柱子,分别标记为 A、B 和 C。初始状态下,柱子 A 上有若干个圆盘,这些圆盘从上到下按从小到大的顺序排列。任务是将这些圆盘全部移到柱子 C 上,且必须保持原有顺序不变。在移动过程中,需要遵守以下规则: 1. 只能从一根柱子的顶部取出圆盘,并将其放入另一根柱子的顶部。 2. 每次只能移动一个圆盘。 3. 小圆盘必须始终在大圆盘之上。 试补全程序。 {{ select(45) }}
  • num = i * i
  • num != 2 * i
  • true
  • false
  1. ①处应填 {{ select(46) }}
  • 0
  • 1
  • 2
  • 3
  1. ②处应填 {{ select(47) }}
  • src, tmp
  • src, tgt
  • tmp, tgt
  • tgt, tmp
  1. ③处应填 {{ select(48) }}
  • src, tmp, tgt
  • src, tgt, tmp
  • tgt, tmp, src
  • tgt, src, tmp
  1. ④处应填 {{ select(49) }}
  • src, tmp, tgt
  • tmp, src, tgt
  • src, tgt, tmp
  • tgt, src, tmp
  1. ⑤处应填( ) ### 2024 年 {{ select(50) }}
  • 0
  • 1
  • i - 1
  • i

2025年

  1. ①处应填 {{ select(51) }}
  • i < z.length()
  • i - 1 >= 0
  • i + 1 < z.length()
  • isdigit(z[i])
  1. ②处应填 {{ select(52) }}
  • count + (z[i] - '0')
  • count * 10 + (z[i] - '0')
  • z[i] - '0'
  • count + 1
  1. ③处应填 {{ select(53) }}
  • count - 1
  • count
  • 10
  • z[i] - '0'
  1. ④处应填 {{ select(54) }}
  • z[i+1]
  • ch
  • z.back()
  • (char)z[i] + 1
#include <iostream>
#include <vector>
using namespace std;

int N;
bool query(int i, int j);

int main() {
    cin >> N;
    int candidate = 0;
    int count = ①;
    for (int i = 1; i < N; ++i) {
        if (②) {
            candidate = i;
            count = 1;
        } else {
            if (③) {
                ④;
            } else {
                count++;
            }
        }
    }
    cout << ⑤ << endl;
    return 0;
}
  1. ⑤处应填 {{ select(55) }}
  • i--
  • i = i + 2
  • i++
  • 不执行任何操作
  1. ①处应填 {{ select(56) }}
  • 0
  • 1
  • N
  • -1
  1. ②处应填 {{ select(57) }}
  • count < 0
  • count == 1
  • count == 0
  • query(candidate, i) == false
  1. ③处应填 {{ select(58) }}
  • query(candidate, i) == false
  • query(i, candidate) == true
  • query(candidate, i) == false && query(i, candidate) == false
  • query(candidate, i) == false || query(i, candidate) == false
  1. ④处应填 {{ select(59) }}
  • count--
  • break
  • count++
  • candidate = i
  1. ⑤处应填 {{ select(60) }}
  • N - 1
  • count
  • candidate
  • 0