#16524. CSP-J初赛阅读程序题合集(2020-2025)

CSP-J初赛阅读程序题合集(2020-2025)

CSP-J初赛阅读程序题合集(2020-2025)

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


2020年

1 #include <cstdlib>
2 #include <iostream>
3 using namespace std;
4 
5 char encoder[26] = {'C','S','P',0};
6 char decoder[26];
7 string st;
8 
9 int main() {
10    int k = 0;
11    for (int i = 0; i < 26; ++i)
12        if (encoder[i] != 0) ++k;
13    for (char x = 'A'; x <= 'Z'; ++x) {
14        bool flag = true;
15        for (int i = 0; i < 26; ++i)
16            if (encoder[i] == x) {
17                flag = false;
18                break;
19            }
20        if (flag) {
21            encoder[k] = x;
22            ++k;
23        }
24    }
25    for (int i = 0; i < 26; ++i)
26        decoder[encoder[i] - 'A'] = i + 'A';
27    cin >> st;
28    for (int i = 0; i < st.length(); ++i)
29        st[i] = decoder[st[i] - 'A'];
30    cout << st;
31    return 0;
32 }
  1. 输入的字符串应当只由大写字母组成,否则在访问数组时可能越界。 {{ select(1) }}
  • 正确
  • 错误
  1. 若输入的字符串不是空串,则输入的字符串与输出的字符串一定不一样。 {{ select(2) }}
  • 正确
  • 错误
  1. 将第 11 行的 i < 26 改为 i < 16,程序运行结果不会改变。 {{ select(3) }}
  • 正确
  • 错误
  1. 将第 25 行的 i < 26 改为 i < 16,程序运行结果不会改变。 {{ select(4) }}
  • 正确
  • 错误
  1. 若输出的字符串为 ABCABCABCA,则下列说法正确的是( )。 {{ select(5) }}
  • 输入的字符串中既有 S 又有 P
  • 输入的字符串中既有 S 又有 B
  • 输入的字符串中既有 A 又有 P
  • 输入的字符串中既有 A 又有 B
  1. 若输出的字符串为 CSPCSPCSPCSP,则下列说法正确的是( )。 {{ select(6) }}
  • 输入的字符串中既有 P 又有 K
  • 输入的字符串中既有 J 又有 R
  • 输入的字符串中既有 J 又有 K
  • 输入的字符串中既有 P 又有 R
1 #include <iostream>
2 using namespace std;
3 
4 long long n, ans;
5 int k, len;
6 long long d[1000000];
7 
8 int main() {
9     cin >> n >> k;
10    d[0] = 0;
11    len = 1;
12    ans = 0;
13    for (long long i = 0; i < n; ++i) {
14        ++d[0];
15        for (int j = 0; j + 1 < len; ++j) {
16            if (d[j] == k) {
17                d[j] = 0;
18                d[j + 1] += 1;
19                ++ans;
20            }
21        }
22        if (d[len - 1] == k) {
23            d[len - 1] = 0;
24            d[len] = 1;
25            ++len;
26            ++ans;
27        }
28    }
29    cout << ans << endl;
30    return 0;
31 }
  1. 若 k=1,则输出 ans 时,len = n。 {{ select(7) }}
  • 正确
  • 错误
  1. 若 k>1,则输出 ans 时,len 一定小于 n。 {{ select(8) }}
  • 正确
  • 错误
  1. 若 k>1,则输出 ans 时,k^len 一定大于 n。 {{ select(9) }}
  • 正确
  • 错误
  1. 若输入的 n 等于 10¹⁵,输入的 k 为 1,则输出等于( )。 {{ select(10) }}
  • 1
  • (10³⁰ − 10¹⁵) / 2
  • (10³⁰ + 10¹⁵) / 2
  • 10¹⁵
  1. 若输入的 n 等于 205,891,132,094,649(即 3³⁰),输入的 k 为 3,则输出等于( )。 {{ select(11) }}
  • 3³⁰
  • (3³⁰ − 1) / 2
  • 3³⁰ − 1
  • (3³⁰ + 1) / 2
  1. 若输入的 n 等于 100,010,002,000,090,输入的 k 等于 10,则输出等于( )。 {{ select(12) }}
  • 11,112,222,444,543
  • 11,122,222,444,453
  • 11,122,222,444,543
  • 11,112,222,444,453
1 #include <algorithm>
2 #include <iostream>
3 using namespace std;
4 
5 int n;
6 int d[50][2];
7 int ans;
8 
9 void dfs(int n, int sum) {
10    if (n == 1) {
11        ans = max(sum, ans);
12        return;
13    }
14    for (int i = 1; i < n; ++i) {
15        int a = d[i-1][0], b = d[i-1][1];
16        int x = d[i][0], y = d[i][1];
17        d[i-1][0] = a + x;
18        d[i-1][1] = b + y;
19        for (int j = i; j < n-1; ++j)
20            d[j][0] = d[j+1][0], d[j][1] = d[j+1][1];
21        int s = a + x + abs(b - y);
22        dfs(n-1, sum + s);
23        for (int j = n-1; j > i; --j)
24            d[j][0] = d[j-1][0], d[j][1] = d[j-1][1];
25        d[i-1][0] = a; d[i-1][1] = b;
26        d[i][0] = x; d[i][1] = y;
27    }
28 }
29 
30 int main() {
31    cin >> n;
32    for (int i = 0; i < n; ++i) cin >> d[i][0];
33    for (int i = 0; i < n; ++i) cin >> d[i][1];
34    ans = 0;
35    dfs(n, 0);
36    cout << ans << endl;
37    return 0;
38 }
  1. 若输入的 n 为 0,此程序可能会死循环或发生运行错误。 {{ select(13) }}
  • 正确
  • 错误
  1. 若输入的 n 为 20,接下来的输入全为 0,则输出为 0。 {{ select(14) }}
  • 正确
  • 错误
  1. 输出的数一定不小于输入的 d[i][0] 和 d[i][1] 的任意一个。 {{ select(15) }}
  • 正确
  • 错误
  1. 若输入的 n 为 20,接下来的输入是 20 个 9 和 20 个 0,则输出为( )。 {{ select(16) }}
  • 1890
  • 1881
  • 1908
  • 1917
  1. 若输入的 n 为 30,接下来的输入是 30 个 0 和 30 个 5,则输出为( )。 {{ select(17) }}
  • 2000
  • 2010
  • 2030
  • 2020
  1. 若输入的 n 为 15,接下来输入是 15 到 1,以及 15 到 1,则输出为( )。 ## 三、完善程序题 {{ select(18) }}
  • 2440
  • 2220
  • 2240
  • 2420

2021年

1 #include <iostream>
2 using namespace std;
3 
4 int n;
5 int a[1000];
6 
7 int f(int x) {
8     int ret = 0;
9     for (; x; x &= x - 1) ret++;
10    return ret;
11 }
12 
13 int g(int x) {
14    return x & -x;
15 }
16 
17 int main() {
18    cin >> n;
19    for (int i = 0; i < n; i++) cin >> a[i];
20    for (int i = 0; i < n; i++)
21        cout << f(a[i]) + g(a[i]) << ' ';
22    cout << endl;
23    return 0;
24 }
  1. 输入的 n 等于 1001 时,程序不会发生下标越界。 {{ select(19) }}
  • 正确
  • 错误
  1. 输入的 a[i] 必须全为正整数,否则程序将陷入死循环。 {{ select(20) }}
  • 正确
  • 错误
  1. 当输入为 5 2 1 1 9 1 6 10 时,输出为 3 4 3 1 7 5。 {{ select(21) }}
  • 正确
  • 错误
  1. 当输入为 1 511998 时,输出为 18。 {{ select(22) }}
  • 正确
  • 错误
  1. 将源代码中 g 函数的定义(13-15 行)移到 main 函数的后面,程序可以正常编译运行。 {{ select(23) }}
  • 正确
  • 错误
  1. 当输入为 2 -65536 2147483647 时,输出为( )。 {{ select(24) }}
  • 65532 33
  • 65535 34
  • 65554 33
  • 65552 32
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 
5 char base[64];
6 char table[256];
7 
8 void init() {
9     for (int i = 0; i < 26; i++) base[i] = 'A' + i;
10    for (int i = 0; i < 26; i++) base[26 + i] = 'a' + i;
11    for (int i = 0; i < 10; i++) base[52 + i] = '0' + i;
12    base[62] = '+'; base[63] = '/';
13    for (int i = 0; i < 256; i++) table[i] = 0xff;
14    for (int i = 0; i < 64; i++) table[base[i]] = i;
15    table['='] = 0;
16 }
17 
18 string decode(string str) {
19    string ret;
20    int i;
21    for (i = 0; i < str.size(); i += 4) {
22        ret += table[str[i]] << 2 | table[str[i + 1]] >> 4;
23        if (str[i + 2] != '=')
24            ret += (table[str[i + 1]] & 0x0f) << 4 | table[str[i + 2]] >> 2;
25        if (str[i + 3] != '=')
26            ret += table[str[i + 2]] << 6 | table[str[i + 3]];
27    }
28    return ret;
29 }
30 
31 int main() {
32    init();
33    cout << (int)table[0] << endl;
34    string str;
35    cin >> str;
36    cout << decode(str) << endl;
37    return 0;
38 }
  1. 输出的第二行一定是由小写字母、大写字母、数字和 +、/、= 构成的字符串。 {{ select(25) }}
  • 正确
  • 错误
  1. 可能存在输入不同,但输出的第二行相同的情形。 {{ select(26) }}
  • 正确
  • 错误
  1. 输出的第一行为 -1。 {{ select(27) }}
  • 正确
  • 错误
  1. 设输入字符串长度为 n,decode 函数的时间复杂度为( )。 {{ select(28) }}
  • O(n)
  • O(n log n)
  • O(n²)
  • O(√n)
  1. 当输入为 Y3Nx 时,输出的第二行为( )。 {{ select(29) }}
  • csp
  • csq
  • CSP
  • Csp
  1. 当输入为 Y2NmIDIwMjE= 时,输出的第二行为( )。 {{ select(30) }}
  • ccf2021
  • ccf2022
  • ccf 2021
  • ccf 2022
1 #include <iostream>
2 using namespace std;
3 
4 const int n = 100000;
5 const int N = n + 1;
6 
7 int m;
8 int a[N], b[N], c[N], d[N];
9 int f[N], g[N];
10 
11 void init() {
12    f[1] = g[1] = 1;
13    for (int i = 2; i <= n; i++) {
14        if (!a[i]) {
15            b[m++] = i;
16            c[i] = 1; f[i] = 2;
17            d[i] = 1; g[i] = i + 1;
18        }
19        for (int j = 0; j < m && b[j] * i <= n; j++) {
20            int k = b[j];
21            a[i * k] = 1;
22            if (i % k == 0) {
23                c[i * k] = c[i] + 1;
24                f[i * k] = f[i] / c[i * k] * (c[i * k] + 1);
25                d[i * k] = d[i];
26                g[i * k] = g[i] * k + d[i];
27                break;
28            } else {
29                c[i * k] = 1;
30                f[i * k] = 2 * f[i];
31                d[i * k] = g[i];
32                g[i * k] = g[i] * (k + 1);
33            }
34        }
35    }
36 }
37 
38 int main() {
39    init();
40    int x;
41    cin >> x;
42    cout << f[x] << ' ' << g[x] << endl;
43    return 0;
44 }
  1. 若输入不为 1,把第 12 行删去不会影响输出的结果。 {{ select(31) }}
  • 正确
  • 错误
  1. 第 24 行的 f[i] / c[i * k] 可能存在无法整除而向下取整的情况。 {{ select(32) }}
  • 正确
  • 错误
  1. 在执行完 init() 后,f 数组不是单调递增的,但 g 数组是单调递增的。( ) 单选题 {{ select(33) }}
  • 正确
  • 错误
  1. init 函数的时间复杂度为( )。 {{ select(34) }}
  • O(n log n)
  • O(n √n)
  • O(n²)
  • O(n)
  1. 在执行完 init() 后,f[1], f[2], ..., f[100] 中有( )个等于 2。 {{ select(35) }}
  • 23
  • 24
  • 25
  • 26
  1. 当输入为 1000 时,输出为( )。 ## 三、完善程序题 {{ select(36) }}
  • 15 1340
  • 15 2340
  • 16 1340
  • 16 2340

2022年

1 #include <iostream>
2 using namespace std;
3 
4 int main() {
5     unsigned short x, y;
6     cin >> x >> y;
7     x = (x | x << 2) & 0x33;
8     x = (x | x << 1) & 0x55;
9     y = (y | y << 2) & 0x33;
10    y = (y | y << 1) & 0x55;
11    unsigned short z = x | y << 1;
12    cout << z << endl;
13    return 0;
14 }
  1. 删去第 5 行与第 11 行的 unsigned,程序行为不变。 {{ select(37) }}
  • 正确
  • 错误
  1. 将第 5 行与第 11 行的 short 均改为 char,程序行为不变。 {{ select(38) }}
  • 正确
  • 错误
  1. 程序总是输出一个整数“0”。 {{ select(39) }}
  • 正确
  • 错误
  1. 当输入为 2 2 时,输出为 10。 {{ select(40) }}
  • 正确
  • 错误
  1. 当输入为 2 2 时,输出为 59。 {{ select(41) }}
  • 正确
  • 错误
  1. 当输入为 13 8 时,输出为( ) {{ select(42) }}
  • 0
  • 197
  • 226
  • 209
1 #include <algorithm>
2 #include <iostream>
3 #include <limits>
4 using namespace std;
5 
6 const int MAXN = 105;
7 const int MAXK = 105;
8 int h[MAXN][MAXK];
9 
10 int f(int n, int m) {
11     if (m == 1) return n;
12     if (n == 0) return 0;
13     int ret = numeric_limits<int>::max();
14     for (int i = 1; i <= n; i++)
15         ret = min(ret, max(f(n - i, m), f(i - 1, m - 1)) + 1);
16     return ret;
17 }
18 
19 int g(int n, int m) {
20     for (int i = 1; i <= n; i++)
21         h[i][1] = i;
22     for (int j = 1; j <= m; j++)
23         h[0][j] = 0;
24     for (int i = 1; i <= n; i++) {
25         for (int j = 2; j <= m; j++) {
26             h[i][j] = numeric_limits<int>::max();
27             for (int k = 1; k <= i; k++)
28                 h[i][j] = min(h[i][j], max(h[i - k][j], h[k - 1][j - 1]) + 1);
29         }
30     }
31     return h[n][m];
32 }
33 
34 int main() {
35     int n, m;
36     cin >> n >> m;
37     cout << f(n, m) << endl << g(n, m) << endl;
38     return 0;
39 }
  1. 当输入为 7 3 时,第 15 行用来取最小值的 min 函数执行了 449 次。 {{ select(43) }}
  • 正确
  • 错误
  1. 输出的两行整数总是相同的。 {{ select(44) }}
  • 正确
  • 错误
  1. 当 m 为 1 时,输出的第一行总为 n。 {{ select(45) }}
  • 正确
  • 错误
  1. 算法 g(n, m) 最为准确的时间复杂度分析结果为 {{ select(46) }}
  • O(n^{3/2} m)
  • O(nm)
  • O(n² m)
  • O(n m²)
  1. 当输入为 20 2 时,输出的第一行为 {{ select(47) }}
  • 4
  • 5
  • 6
  • 20
  1. 当输入为 100 100 时,输出的第一行为( ) {{ select(48) }}
  • 6
  • 7
  • 8
  • 9
1 #include <iostream>
2 using namespace std;
3 
4 int n, k;
5 
6 int solve1() {
7     int l = 0, r = n;
8     while (l <= r) {
9         int mid = (l + r) / 2;
10        if (mid * mid <= n) l = mid + 1;
11        else r = mid - 1;
12    }
13    return l - 1;
14 }
15 
16 double solve2(double x) {
17    if (x == 0) return x;
18    for (int i = 0; i < k; i++)
19        x = (x + n / x) / 2;
20    return x;
21 }
22 
23 int main() {
24    cin >> n >> k;
25    double ans = solve2(solve1());
26    cout << ans << ' ' << (ans * ans == n) << endl;
27    return 0;
28 }
  1. 该算法最准确的时间复杂度分析结果为 O(log n + k)。 {{ select(49) }}
  • 正确
  • 错误
  1. 当输入为 9801 1 时,输出的第一个数为 99。 {{ select(50) }}
  • 正确
  • 错误
  1. 对于任意输入的 n,随着所输入 k 的增大,输出的第二个数会变成 1。 {{ select(51) }}
  • 正确
  • 错误
  1. 该程序有存在缺陷。当输入的 n 过大时,第 10 行的乘法有可能溢出,因此应当将 mid 强制转换为 64 位整数再计算。 {{ select(52) }}
  • 正确
  • 错误
  1. 当输入为 2 1 时,输出的第一个数最接近 {{ select(53) }}
  • 1
  • 1.414
  • 1.5
  • 2
  1. 当输入为 3 10 时,输出的第一个数最接近 {{ select(54) }}
  • 1.7
  • 1.732
  • 1.75
  • 2
  1. 当输入为 256 11 时,输出的第一个数( ) ## 三、完善程序题 {{ select(55) }}
  • 等于 16
  • 接近但大于 16
  • 接近但小于 16
  • 前三种情况都有可能

2023年

1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4 
5 double f(double a, double b, double c) {
6     double s = (a + b + c) / 2;
7     return sqrt(s * (s - a) * (s - b) * (s - c));
8 }
9 
10 int main() {
11     cout.flags(ios::fixed);
12     cout.precision(4);
13     int a, b, c;
14     cin >> a >> b >> c;
15     cout << f(a, b, c) << endl;
16     return 0;
17 }
  1. 将第 7 行中的 (s - b) * (s - c) 改为 (s - c) * (s - b) 不会影响程序运行的结果。 {{ select(56) }}
  • 正确
  • 错误
  1. 当输入为 3 4 5 时,输出为 {{ select(57) }}
  • 6.0000
  • 12.0000
  • 24.0000
  • 30.0000
  1. 程序总是输出四位小数。 {{ select(58) }}
  • 正确
  • 错误
  1. 当输入为 5 12 13 时,输出为 {{ select(59) }}
  • 24.0000
  • 30.0000
  • 60.0000
  • 120.0000
  1. 当输入为 2 2 2 时,输出为 1.7321。 {{ select(60) }}
  • 正确
  • 错误
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 using namespace std;
5 
6 int f(string x, string y) {
7     int m = x.size();
8     int n = y.size();
9     vector<vector<int>> v(m + 1, vector<int>(n + 1, 0));
10    for (int i = 1; i <= m; i++) {
11        for (int j = 1; j <= n; j++) {
12            if (x[i - 1] == y[j - 1]) {
13                v[i][j] = v[i - 1][j - 1] + 1;
14            } else {
15                v[i][j] = max(v[i - 1][j], v[i][j - 1]);
16            }
17        }
18    }
19    return v[m][n];
20 }
21 
22 bool g(string x, string y) {
23    if (x.size() != y.size()) {
24        return false;
25    }
26    return f(x + x, y) == y.size();
27 }
28 
29 int main() {
30    string x, y;
31    cin >> x >> y;
32    cout << g(x, y) << endl;
33    return 0;
34 }
  1. f 函数的返回值小于等于 min(n, m)。 {{ select(61) }}
  • 正确
  • 错误
  1. f 函数的返回值等于两个输入字符串的最长公共子串的长度。 {{ select(62) }}
  • 正确
  • 错误
  1. 当输入两个完全相同的字符串时,g 函数的返回值总是 true。 {{ select(63) }}
  • 正确
  • 错误
  1. 将第 19 行中的 v[m][n] 替换为 v[n][m],那么该程序 {{ select(64) }}
  • 行为不变
  • 只会改变输出
  • 一定非正常退出
  • 可能非正常退出
  1. 当输入为 csp-j p-jcs 时,输出为 {{ select(65) }}
  • 0
  • 1
  • T
  • F
  1. 当输入为 csppsc spsccp 时,输出为( ) {{ select(66) }}
  • T
  • F
  • 0
  • 1
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4 
5 int solve1(int n) {
6     return n * n;
7 }
8 
9 int solve2(int n) {
10    int sum = 0;
11    for (int i = 1; i <= sqrt(n); i++) {
12        if (n % i == 0) {
13            if (n / i == i) {
14                sum += i * i;
15            } else {
16                sum += i * i + (n / i) * (n / i);
17            }
18        }
19    }
20    return sum;
21 }
22 
23 int main() {
24    int n;
25    cin >> n;
26    cout << solve2(solve1(n)) << " " << solve1(solve2(n)) << endl;
27    return 0;
28 }
  1. 如果输入的 n 为正整数,solve2 函数的作用是计算 n 所有的因子的平方和。 {{ select(67) }}
  • 正确
  • 错误
  1. 第 13~14 行的作用是避免 n 的平方根因子 i(或 n / i)进入第 16 行而被计算两次。 {{ select(68) }}
  • 正确
  • 错误
  1. 如果输入的 n 为质数 p 的平方,那么 solve2(n) 的返回值为 {{ select(69) }}
  • p2+p+1p^{2} + p + 1
  • n2+n+1n^{2} + n + 1
  • n2+1n^{2} + 1
  • p4+2p2+1p^{4} + 2p^{2} + 1
  1. 如果输入的 n 为质数,solve2(n) 的返回值为 n2+1n^{2} + 1。 {{ select(70) }}
  • 正确
  • 错误
  1. 当输入为正整数时,第一项减去第二项的差值一定 {{ select(71) }}
  • 大于 0
  • 大于等于 0 且不一定大于 0
  • 小于 0
  • 小于等于 0 且不一定小于 0
  1. 当输入为 5 时,输出为( ) ## 三、完善程序题 {{ select(72) }}
  • 651625
  • 650729
  • 651676
  • 652625

2024年

#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n <= 1) {
        return false;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int countPrimes(int n) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
        if (isPrime(i)) {
            count++;
        }
    }
    return count;
}

int sumPrimes(int n) {
    int sum = 0;
    for (int i = 2; i <= n; i++) {
        if (isPrime(i)) {
            sum += i;
        }
    }
    return sum;
}

int main() {
    int x;
    cin >> x;
    cout << countPrimes(x) << " " << sumPrimes(x) << endl;
    return 0;
}
  1. 当输入为 10 时,程序的第一个输出为 4,第二个输出为 17。 {{ select(73) }}
  • 正确
  • 错误
  1. 若将 isPrime(i) 函数中的条件 i * i <= n 改为 i < n / 2,输入 20 时,countPrimes(20) 的输出将变为 6。 {{ select(74) }}
  • 正确
  • 错误
  1. sumPrimes 函数计算的是从 2 到 n 之间的所有素数之和。 {{ select(75) }}
  • 正确
  • 错误
  1. 当输入为 50 时,sumPrimes(50) 的输出为( )。 {{ select(76) }}
  • 1060
  • 328
  • 381
  • 275
  1. 如果将 for(int i = 2; i * i <= n; i++) 改为 for(int i = 2; i <= n; i++),输入 10 时,程序的输出( )。 {{ select(77) }}
  • 将不能正确计算 10 以内素数个数及其和
  • 仍然输出 4 和 17
  • 输出 3 和 10
  • 输出结果不变,但运行时间更短
#include <iostream>
#include <vector>
using namespace std;

int compute(vector<int> & cost) {
    int n = cost.size();
    vector<int> dp(n + 1, 0);
    dp[1] = cost[0];
    for (int i = 2; i <= n; i++) {
        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i - 1];
    }
    return min(dp[n], dp[n - 1]);
}

int main() {
    int n;
    cin >> n;
    vector<int> cost(n);
    for (int i = 0; i < n; i++) {
        cin >> cost[i];
    }
    cout << compute(cost) << endl;
    return 0;
}
  1. 当输入的 cost 数组为 10 15 20 时,程序的输出为 15。 {{ select(78) }}
  • 正确
  • 错误
  1. 如果将 dp[i-1] 改为 dp[i-3],程序可能会产生编译错误。 {{ select(79) }}
  • 正确
  • 错误
  1. 程序总是输出 cost 数组中的最小的元素。 {{ select(80) }}
  • 正确
  • 错误
  1. 当输入的 cost 数组为 {1, 100, 1, 1, 1, 100, 1, 1, 100, 1} 时,程序的输出为( )。 {{ select(81) }}
  • 6
  • 7
  • 8
  • 9
  1. 如果输入的 cost 数组为 {10, 15, 30, 5, 5, 10, 20},程序的输出为( )。 {{ select(82) }}
  • 25
  • 30
  • 35
  • 40
  1. 若将代码中的 min(dp[i-1], dp[i-2]) + cost[i-1] 修改为 dp[i-1] + cost[i-2],输入 cost 数组为 {5, 10, 15} 时,程序的输出为( )。 {{ select(83) }}
  • 10
  • 15
  • 20
  • 25
#include <iostream>
#include <cmath>
using namespace std;

int customFunction(int a, int b) {
    if (b == 0) {
        return a;
    }
    return a + customFunction(a, b - 1);
}

int main() {
    int x, y;
    cin >> x >> y;
    int result = customFunction(x, y);
    cout << pow(result, 2) << endl;
    return 0;
}
  1. 当输入为 2 3 时,customFunction(2,3) 的返回值为 64。 {{ select(84) }}
  • 正确
  • 错误
  1. 当 b 为负数时,customFunction(a, b) 会陷入无限递归。 {{ select(85) }}
  • 正确
  • 错误
  1. 当 b 的值越大,程序的运行时间越长。 {{ select(86) }}
  • 正确
  • 错误
  1. 当输入为 5 4 时,customFunction(5,4) 的返回值为( )。 {{ select(87) }}
  • 5
  • 25
  • 250
  • 625
  1. 如果输入 x = 3 和 y = 3,则程序的最终输出为( )。 {{ select(88) }}
  • 27
  • 81
  • 144
  • 256
#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. 若将 customFunction 函数改为 return a + customFunction(a-1, b-1); 并输入 3 3,则程序的最终输出为( )。 ### 三、完善程序题 判断平方数 问题:给定一个正整数 n,判断这个数是不是完全平方数,即存在一个正整数 x 使得 x 的平方等于 n。试补全程序。 {{ select(89) }}
  • 9
  • 16
  • 25
  • 36

2025年

  1. 当输入为2时,程序并不会执行第16行的判断语句。 {{ select(90) }}
  • 正确
  • 错误
  1. 将第16行中的&& gcd(i, k) == 1删去不会影响程序运行结果。 {{ select(91) }}
  • 正确
  • 错误
  1. 当输入的n≥3的时候,程序总是输出一个正整数。 {{ select(92) }}
  • 正确
  • 错误
  1. 将第7行的gcd(b, a % b)改为gcd(a, a % b)后,程序可能出现的问题是( )。 {{ select(93) }}
  • 输出的答案大于原答案。
  • 输出的答案小于原答案。
  • 程序有可能陷入死循环。
  • 可能发生整型溢出问题。
  1. 当输入为8的时候,输出为( )。 {{ select(94) }}
  • 37
  • 42
  • 35
  • 25
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long

int n, k;
int a[20000];
int ans[20007];

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    std::sort(a + 1, a + n + 1);
    n = std::unique(a + 1, a + n + 1) - a - 1;
    for (int i = 1, j = 0; i <= n; ++i) {
        for (; j < i && a[i] - a[j + 1] > k; ++j);
        ans[i] = ans[j] + 1;
    }
    printf("%d\n", ans[n]);
    return 0;
}
  1. 调用gcd(36, 42)会返回( )。 {{ select(95) }}
  • 6
  • 252
  • 3
  • 2
  1. 当输入为"3 1 3 2 1"时,输出结果为2。 {{ select(96) }}
  • 正确
  • 错误
  1. 假设输入的n为正整数,输出的答案一定小于等于n,大于等于1。 {{ select(97) }}
  • 正确
  • 错误
  1. 将第14行的n = std::unique(a+1, a+n+1) - a - 1;删去后,有可能出现与原本代码不同的输出结果。 {{ select(98) }}
  • 正确
  • 错误
  1. 假设输入的a数组和k均为正整数,执行第18行代码时,一定满足的条件不包括( )。 {{ select(99) }}
  • j <= i
  • a[i] - a[j] > k
  • j <= n
  • a[j] < a[i]
  1. 当输入的n=100、k=2、a={1,2,...,100}时,输出为( )。 {{ select(100) }}
  • 34
  • 100
  • 50
  • 33
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long

int f[5007][5007];
int a[5007], b[5007];
int n;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &b[i]);
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            f[i][j] = std::max(f[i][j], std::max(f[i-1][j], f[i][j-1]));
            if (a[i] == b[j]) {
                f[i][j] = std::max(f[i][j], f[i-1][j-1] + 1);
            }
        }
    }
    printf("%d\n", f[n][n]);
    return 0;
}
  1. 假设输入的a数组和k均为正整数,但a数组不一定有序,若误删去第13行的std::sort(a+1, a+n+1);,程序有可能出现的问题有( )。 {{ select(101) }}
  • 输出的答案比原本答案更大
  • 输出的答案比原本答案更小
  • 出现死循环行为
  • 以上均可能发生
  1. 当输入4 1 2 3 4 1 3 2 2时,输出为2。 {{ select(102) }}
  • 正确
  • 错误
  1. 当程序运行完毕后,对于所有的1≤i,j≤n,都一定有f[i][j]≤f[n][n]。 {{ select(103) }}
  • 正确
  • 错误
  1. 将第18行的f[i][j] = std::max(f[i][j], std::max(f[i-1][j], f[i][j-1]));删去后,并不影响程序运行结果。 {{ select(104) }}
  • 正确
  • 错误
  1. 输出的答案满足的性质有( )。 {{ select(105) }}
  • 小于等于n
  • 大于等于0
  • 不一定大于等于1
  • 以上均是
  1. 如果在第16行的循环前加上以下两行:std::sort(a+1, a+n+1); std::sort(b+1, b+n+1);,则答案会( )。 {{ select(106) }}
  • 变大或不变
  • 变小或不变
  • 一定变大
  • 不变
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string z;
    cin >> z;
    string s = "";
    for (int i = 0; i < z.length(); ) {
        char ch = z[i];
        if (__①__ && isdigit(z[i+1])) {
            int count = 0;
            i++;
            while (i < z.length() && isdigit(z[i])) {
                count = __②__;
                i++;
            }
            for (int j = 0; j < __③__; ++j) {
                s += ch;
            }
        } else {
            s += __④__;
            __⑤__;
        }
    }
    cout << s << endl;
    return 0;
}
  1. 如果输入的a={1,2,...,n},而且b数组中数字均为1~n中的正整数,则上述代码等价于下面哪个问题( )。 {{ select(107) }}
  • 求b数组去重后的长度
  • 求b数组的最长上升子序列
  • 求b数组的长度
  • 求b数组的最大值