ARTICLE · 1005218
2026年 CSP-J第一轮模拟题(含解析)
2026年 CCF非专业级别软件能力认证第一轮
(CSP-J)入门级C++语言模拟题
考试的时候不得使用任何电子设备(如计算器、手机、电子词典等)或查阅任何书籍资料。
一、单项选择题
1. 世界上第一台通用电子计算机的名称是()。
A. ENIAC
B. UNIVAC
C. EDVAC
D. Mark I
2. 在 C++ 中,sizeof(char) 的值是( )。
A. 2
B. 4
C. 1
D. 与编译器有关
3. 十六进制数 0xAB 转换为十进制是( )。
A. 161
B. 171
C. 176
D. 187
4. 在 C++ 中,unsigned int 类型能表示的最大值约为( )。
A. 2^31-1
B. 2^32-1
C. 2^15-1
D. 2^16-1
5. 设栈的输入序列为 a, b, c, d, e,以下哪个不是合法的出栈序列?( )
A. b, a, e, d, c
B. a, c, b, d, e
C. c, b, a, d, e
D. d, a, b, c, e
6. 一个有 n 个顶点的无向连通图,其最小生成树有( )条边。
A. n
B. n-1
C. n+1
D. n(n-1)/2
7. 给定 5 个权值 {1, 2, 3, 4, 5},构建哈夫曼树,其带权路径长度(WPL)为( )。
A. 28
B. 30
C. 33
D. 35
8. 以下排序算法中,平均时间复杂度为 O(n log n) 的是( )。
A. 冒泡排序
B. 插入排序
C. 归并排序
D. 选择排序
9. 表达式 (a + b) * c - d 对应的前缀表达式是( )。
A. - * + a b c d
B. a b + c * d -
C. - + * a b c d
D. * + a b - c d
10. 设 x = 10(二进制 1010),则 (x >> 1) & x 的结果是( )。
A. 0
B. 2
C. 5
D. 10
11. 从 5 男 4 女中选 3 人,要求至少有 1 名女生,共有多少种选法?( )
A. 74
B. 84
C. 40
D. 50
12. 一棵有 2024 个节点的二叉树,最多有多少个叶子节点?( )
A. 1010
B. 1011
C. 1012
D. 1013
13. 在单链表中,删除 p 所指节点的后继节点 q 的正确操作是( )。
A. p->next = q->next; delete q;
B. q->next = p->next; delete q;
C. p->next = q; delete q;
D. p = q->next; delete q;
14. 关于动态规划,以下说法正确的是()。
A. 动态规划必须使用递归实现
B. 动态规划的核心是将问题分解为重叠子问题并保存子问题的解
C. 动态规划只能用于求最优解的问题
D. 动态规划的时间复杂度一定比暴力搜索低
15. 在 n 个顶点 m 条边的无向图中,用邻接表存储,则所有顶点的邻接表中节点总数为( )。
A. n
B. m
C. 2m
D. 2n
二、阅读程序题
第 1 题 阅读以下程序:
#include<iostream>using namespace std;intmain(){int n, a[1005], s[1005] = {};cin >> n;for (int i = 1; i <= n; i++) {cin >> a[i];s[i] = s[i - 1] + a[i];}int q;cin >> q;while (q--) {int l, r;cin >> l >> r;cout << s[r] - s[l - 1] << endl;}return 0;}
判断题
16.该程序计算的是数组指定区间 [l, r] 的元素之和。( )
17. 数组 s 存储的是前缀和,s[i] 表示 a[1] 到 a[i] 的累加值。( )
18. 如果输入的 l > r,程序会输出 0。( )
19. 程序的时间复杂度为 O(n + q)。( )
选择题
20. 当查询区间为 1 3 时,输出是?( ) A. 7 B. 8 C. 4 D. 3
21. 当查询区间为 2 5 时,输出是?( ) A. 14 B. 10 C. 11 D. 12
第 2 题 阅读以下程序:
#include<iostream>using namespace std;intgcd(int a, int b){if (b == 0) return a;return gcd(b, a % b);}intlcm(int a, int b){return a / gcd(a, b) * b;}intmain(){int a, b;cin >> a >> b;cout << gcd(a, b) << endl;cout << lcm(a, b) << endl;return 0;}
判断题
22. gcd 函数使用的是辗转相除法(欧几里得算法)。( )
23. gcd(12, 18) 的返回值为 6。( )
24. lcm(12, 18) 的输出结果为 24。( )
25. 如果输入为 7 和 13(两个质数),gcd 的返回值为 1。( )
选择题
26. gcd(12, 18) 的递归调用次数(包括初始调用)是?( ) A. 3 B. 4 C. 5 D. 6
27. 如果输入为 100 60,gcd 的输出是?( ) A. 10 B. 5 C. 20 D. 60
第 3 题 阅读以下程序:
#include<iostream>#include<string>using namespace std;intmain(){string s;int k;cin >> s >> k;k = k % 26;for (int i = 0; i < s.length(); i++) {if (s[i] >= 'a' && s[i] <= 'z') {s[i] = (s[i] - 'a' + k) % 26 + 'a';} else if (s[i] >= 'A' && s[i] <= 'Z') {s[i] = (s[i] - 'A' + k) % 26 + 'A';}}cout << s << endl;return 0;}
判断题
28. 该程序实现的是凯撒密码加密,将每个字母向后移动 k 位。( )
29. 程序中 k = k % 26 的作用是处理 k 大于 26 的情况。( )
30. 如果输入字符串包含数字,数字也会被加密修改。( )
31. 当 k = 0 时,输出字符串与输入字符串完全相同。( )
选择题
32. 当输入为 Hello 3 时,输出是?( ) A. Khoor B. Ebiil C. Ifmmp D. Lhoor
33. 当输入为 xyz 3 时,输出是?( ) A. abc B. yza C. wxy D. bcd
三、完善程序题
第 1 题 活动安排问题
题目描述:有 n 个活动,每个活动有开始时间 s[i] 和结束时间 e[i]。一个场地同一时间只能举办一个活动,活动之间不能重叠(一个活动结束的时刻,另一个活动可以立即开始)。求最多能安排多少个不重叠的活动。
#include<iostream>#include<algorithm>using namespace std;struct Activity {int start, end;};boolcmp(Activity a, Activity b){return ①______;}intmain(){int n;cin >> n;Activity act[1005];for (int i = 0; i < n; i++)cin >> act[i].start >> act[i].end;②______; // 排序int count = 1;int lastEnd = ③______;for (int i = 1; i < n; i++) {if (act[i].start ④______) {count++;⑤______;}}cout << count << endl;return 0;}
34.① 处应填( ) A. a.start < b.start B. a.end < b.end C. a.end > b.end D. a.start > b.start
35.② 处应填( ) A. sort(act, act+n) B. sort(act, act+n, cmp) C. sort(act+1, act+n+1, cmp) D. sort(act, act+n-1, cmp)
36.③ 处应填( ) A. act[0].start B. act[0].end C. act[n-1].end D. 0
37.④ 处应填( ) A. >= lastEnd B. > lastEnd C. < lastEnd D. <= lastEnd
38.⑤ 处应填( ) A. lastEnd = act[i].start B. lastEnd = act[i].end C. lastEnd = act[i-1].end D. count = act[i].end
第 2 题 最长上升子序列
题目描述:给定一个长度为 n 的整数序列,求其最长严格上升子序列的长度。例如序列 [3, 1, 2, 1, 8, 5, 6] 的最长上升子序列为 [1, 2, 5, 6],长度为 4。
#include<iostream>using namespace std;intmain(){int n, a[5005], dp[5005];cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];①______; // 初始化 dp[i]}int ans = 1;for (int i = 1; i < n; i++) {for (int j = 0; j < i; j++) {if (②______) {if (dp[j] + 1 > dp[i])③______;}}if (④______)ans = dp[i];}cout << ⑤______ << endl;return 0;}
39.① 处应填( ) A. dp[i] = 0 B. dp[i] = 1 C. dp[i] = a[i] D. dp[i] = i
40.② 处应填( ) A. a[j] < a[i] B. a[j] > a[i] C. a[j] <= a[i] D. a[j] >= a[i]
41.③ 处应填( ) A. dp[i] = dp[j] + 1 B. dp[i] = dp[j] C. dp[j] = dp[i] + 1 D. dp[i] = dp[i] + 1
42.④ 处应填( ) A. dp[i] > ans B. dp[i] < ans C. dp[i] == ans D. dp[i] > dp[j]
43.⑤ 处应填( ) A. dp[n] B. dp[n-1] C. ans D. dp[ans]
答案
一、单项选择题答案速查
题号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
答案 | A | C | B | B | D | B | C | C | A | A | A | C | A | B | C |
二、阅读程序题答案速查
题号 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
答案 | √ | √ | × | √ | B | C | √ | √ | × | √ | B | C | √ | √ | × | √ | A | A |
三、完善程序题答案
题号 | ① | ② | ③ | ④ | ⑤ |
第1题 | B | B | B | A | B |
第2题 | B | A | A | A | C |