#10119. CSP-J2026 初赛模拟卷03
CSP-J2026 初赛模拟卷03
一、单项选择题(共15题,每题2分,共计30分;每题有且仅有一个正确选项)
- 阅读下面代码,输出结果是?
#include <iostream>
using namespace std;
int x = 5;
void f() {
int x = 3;
x += 2;
}
int main() {
f();
cout << x;
return 0;
}
{{ select(1) }}
- 3
- 5
- 7
- 程序无法通过编译
- 十六进制数 与二进制数 的和对应的十进制值是?
{{ select(2) }}
- 86
- 90
- 92
- 94
- 阅读下面代码,输出结果是?
int a[4] = {2, 4, 6, 8};
int *p = a;
cout << *(p + 2);
{{ select(3) }}
- 2
- 4
- 6
- 8
- 一个队列初始为空,依次执行如下操作:
push 4
push 7
pop
push 9
pop
push 1
此时队列中从队首到队尾的元素依次为?
{{ select(4) }}
- 一棵完全二叉树用数组存储,根结点存储在下标 1 的位置。若某结点下标为 12,则它的父结点下标和左儿子下标分别为?
{{ select(5) }}
- 一个无向图有 9 个顶点、12 条边,则所有顶点的度数之和为?
{{ select(6) }}
- 12
- 18
- 21
- 24
- 有向无环图中有边 。该图共有多少种不同的拓扑排序?
{{ select(7) }}
- 3
- 4
- 5
- 6
- 中缀表达式 对应的前缀表达式是?
{{ select(8) }}
*+ab-cd+a*b-cdab+cd-**-+abcd
- 由字母
A, A, B, C, D组成的不同字符串共有多少个?
{{ select(9) }}
- 30
- 45
- 60
- 120
- 递归函数定义如下:
f(0)=1
f(1)=1
f(n)=f(n-1)+2*f(n-2) (n>=2)
则 的值为?
{{ select(10) }}
- 11
- 15
- 21
- 31
- 阅读下面代码,输出结果是?
int x = 3;
int *p = &x;
(*p)++;
cout << x;
{{ select(11) }}
- 2
- 3
- 4
- 程序无法通过编译
- 下面代码片段的时间复杂度是?
for (int i = 1; i <= n; i *= 2)
for (int j = 1; j <= n; j++)
cout << i + j << endl;
{{ select(12) }}
- 按顺序向一棵空二叉搜索树中插入 ,则该二叉搜索树的后序遍历为?
{{ select(13) }}
- 有 5 个字符,其出现频率分别为 。若使用哈夫曼编码,则频率为 2 的字符编码长度为?
{{ select(14) }}
- 1
- 2
- 3
- 4
- 下面哪个 STL 容器最适合描述"先进先出"的数据结构?
{{ select(15) }}
stackqueuesetmap
二、阅读程序(程序输入不超过数组或字符串定义的范围;判断题正确填 √,错误填 ×;除特殊说明外,判断题每题 1.5 分,选择题每题 3 分,共计 40 分) (1)
#include <iostream>
#include <string>
using namespace std;
string rot(string s, int k) {
int n = s.size();
k %= n;
string t;
for (int i = k; i < n; i++)
t += s[i];
for (int i = 0; i < k; i++)
t += s[i];
return t;
}
int main() {
string s;
int k;
cin >> s >> k;
cout << rot(s, k) << endl;
return 0;
}
假设输入字符串非空,且只包含小写字母和数字。
判断题
- 当输入为
abcde 2时,程序输出为cdeab。
{{ select(16) }}
- 正确
- 错误
- 当 是字符串长度的倍数时,程序输出一定与原字符串相同。
{{ select(17) }}
- 正确
- 错误
- 该程序实现的是将字符串向右循环移动 位。
{{ select(18) }}
- 正确
- 错误
选择题
- 当输入为
cspj2026 10时,程序输出为?
{{ select(19) }}
cspj202626cspj20pj2026cs2026cspj
- 设输入字符串长度为 ,该程序的时间复杂度为?
{{ select(20) }}
(2)
#include <iostream>
using namespace std;
int v[105], w[105], dp[1005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> v[i] >> w[i];
for (int i = 1; i <= n; i++) {
for (int j = m; j >= v[i]; j--) {
if (dp[j] < dp[j - v[i]] + w[i])
dp[j] = dp[j - v[i]] + w[i];
}
}
cout << dp[m] << endl;
return 0;
}
假设 ,,且所有 均为正整数。
判断题
- 第 13 行的循环从大到小枚举 ,可以保证每个物品最多被选一次。
{{ select(21) }}
- 正确
- 错误
- 若将第 13 行改为从 到 递增枚举 ,程序仍然总能求出每个物品最多选一次的最优值。
{{ select(22) }}
- 正确
- 错误
- 程序中的 表示恰好装满容量 时的最大价值。
{{ select(23) }}
- 正确
- 错误
选择题
- 若输入为:
3 5
2 3
3 4
4 5
程序输出为?
{{ select(24) }}
- 5
- 7
- 9
- 12
- 若输入为:
3 4
2 10
2 20
3 25
程序输出为?
{{ select(25) }}
- 20
- 25
- 30
- 35
- 该程序的时间复杂度为?
{{ select(26) }}
(3)
#include <iostream>
using namespace std;
int n, ans;
void dfs(int p, int la) {
if (p > n) {
ans++;
return;
}
dfs(p + 1, 0);
if (!la)
dfs(p + 1, 1);
}
int main() {
cin >> n;
dfs(1, 0);
cout << ans << endl;
return 0;
}
假设输入的 是不超过 20 的正整数。
判断题
- 当输入为
1时,程序输出为2。
{{ select(27) }}
- 正确
- 错误
- 当输入为
3时,程序输出为5。
{{ select(28) }}
- 正确
- 错误
- 当输入为
4时,程序输出为7。
{{ select(29) }}
- 正确
- 错误
选择题
- 当输入为
5时,程序输出为?
{{ select(30) }}
- 8
- 13
- 16
- 21
- 若删去第 13 行的
if (!la)判断,直接执行dfs(p + 1, 1);,当输入为4时,程序输出为?
{{ select(31) }}
- 8
- 10
- 15
- 16
- 当输入为
6时,程序输出为?
{{ select(32) }}
- 18
- 20
- 21
- 32
三、完善程序(单选题,每题 3 分,共计 30 分)
(1)下面程序使用选择排序将数组从小到大排序,请补全程序。
#include <iostream>
using namespace std;
int a[1005];
void sel(int n) {
for (int i = 0; i < n; i++) {
int p = ①;
for (int j = ②; j < n; j++) {
if (③)
p = j;
}
int t = a[i];
a[i] = ④;
⑤ = t;
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
sel(n);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}
- ① 处应填?
{{ select(33) }}
i0i + 1n - 1
- ② 处应填?
{{ select(34) }}
0ii + 1n
- ③ 处应填?
{{ select(35) }}
a[j] < a[p]a[j] > a[p]j < pa[i] < a[j]
- ④ 处应填?
{{ select(36) }}
a[i]a[p]a[j]t
- ⑤ 处应填?
{{ select(37) }}
a[i]a[p]a[j]p
(2)下面程序输出所有长度为 的合法括号序列,请补全程序。
#include <iostream>
#include <string>
using namespace std;
int n;
void dfs(int l, int r, string s) {
if (①) {
cout << s << endl;
return;
}
if (②)
dfs(③, r, s + "(");
if (④)
dfs(l, ⑤, s + ")");
}
int main() {
cin >> n;
dfs(0, 0, "");
return 0;
}
其中 表示已经放入的左括号数量, 表示已经放入的右括号数量。
- ① 处应填?
{{ select(38) }}
l == rl == nr == nl == n && r == n
- ② 处应填?
{{ select(39) }}
l < nl > nr < nr > l
- ③ 处应填?
{{ select(40) }}
l - 1l + 1r + 1n
- ④ 处应填?
{{ select(41) }}
l < rl == rr < lr == n
- ⑤ 处应填?
{{ select(42) }}
r - 1r + 1l + 1n