#10126. CSP-J2026 初赛模拟卷09
CSP-J2026 初赛模拟卷09
一、单项选择题(共15题,每题2分,共计30分;每题有且仅有一个正确选项)
- 阅读下面代码,输出结果是?
#include <iostream>
using namespace std;
int f() {
static int x = 1;
x += 2;
return x;
}
int main() {
cout << f() << " " << f();
return 0;
}
{{ select(1) }}
1 33 55 73 3
- 五进制数 与八进制数 的和对应的十进制值是?
{{ select(2) }}
- 75
- 76
- 77
- 78
- 阅读下面代码,输出结果是?
void f(int &a, int b) {
a += b;
b += a;
}
int main() {
int x = 2, y = 3;
f(x, y);
cout << x << " " << y;
return 0;
}
{{ select(3) }}
2 35 35 82 8
- 阅读下面代码,输出结果是?
string s = "abacaba";
cout << s.find("aca");
{{ select(4) }}
- 0
- 1
- 2
- 3
- 一个空栈依次将 入栈,下面哪个序列不可能作为出栈序列?
{{ select(5) }}
- 一棵有 2026 个结点的完全二叉树,叶子结点个数为?
{{ select(6) }}
- 1011
- 1012
- 1013
- 1014
- 有向无环图中有边 。该图共有多少种不同的拓扑排序?
{{ select(7) }}
- 3
- 4
- 5
- 6
- 有 6 个字符,其出现频率分别为 。若使用哈夫曼编码,则频率为 2 的字符编码长度为?
{{ select(8) }}
- 1
- 2
- 3
- 4
- 从 中选出 3 个数,要求任意两个数都不相邻,共有多少种选法?
{{ select(9) }}
- 6
- 7
- 9
- 10
- 递归函数定义如下:
f(1)=1
f(n)=f(n-1)+n*(n-1) (n>=2)
{{ select(10) }}
- 31
- 41
- 51
- 61
- 下面代码片段的时间复杂度是?
for (int i = 1; i <= n; i++)
for (int j = 1; j * j <= i; j++)
cout << i + j << endl;
{{ select(11) }}
- 若
int占 4 字节,则数组int a[50][80];大约占用多少字节?
{{ select(12) }}
- 8000
- 16000
- 32000
- 4000
- 一个无向森林有 20 个顶点、5 个连通块,则它有多少条边?
{{ select(13) }}
- 14
- 15
- 16
- 19
- 阅读下面代码,输出结果是?
vector<int> v;
v.push_back(3);
v.push_back(1);
v.pop_back();
cout << v.size() << " " << v[0];
{{ select(14) }}
1 31 12 32 1
- 阅读下面代码,输出结果是?
cout << (3 + 4 * 2 > 10 && 5);
{{ select(15) }}
- 0
- 1
- 5
- 11
二、阅读程序(程序输入不超过数组或字符串定义的范围;判断题正确填 √,错误填 ×;除特殊说明外,判断题每题 1.5 分,选择题每题 3 分,共计 40 分)
阅读程序(一)
#include <iostream>
using namespace std;
int a[1005];
int ub(int n, int x) {
int l = 1, r = n + 1;
while (l < r) {
int m = (l + r) / 2;
if (a[m] <= x)
l = m + 1;
else
r = m;
}
return l - 1;
}
int main() {
int n, x;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> x;
cout << ub(n, x) << endl;
return 0;
}
假设 单调不降,且 。
判断题
- 如果所有 都小于等于 ,程序输出 。
{{ select(16) }}
- 正确
- 错误
- 函数
ub返回的是第一个大于等于 的位置。
{{ select(17) }}
- 正确
- 错误
- 如果所有 都大于 ,程序输出 。
{{ select(18) }}
- 正确
- 错误
单项选择题
- 若输入为:
5
1 2 2 4 7
2
{{ select(19) }}
- 1
- 2
- 3
- 4
- 该程序中函数
ub的时间复杂度为?
{{ select(20) }}
阅读程序(二)
#include <iostream>
#include <algorithm>
using namespace std;
int g[105][105], c[105], d[105], dp[105];
int q[105];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u][++c[u]] = v;
d[v]++;
}
int hd = 0, tl = 0;
for (int i = 1; i <= n; i++) {
dp[i] = 1;
if (d[i] == 0)
q[tl++] = i;
}
while (hd < tl) {
int u = q[hd++];
for (int i = 1; i <= c[u]; i++) {
int v = g[u][i];
if (dp[v] < dp[u] + 1)
dp[v] = dp[u] + 1;
d[v]--;
if (d[v] == 0)
q[tl++] = v;
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, dp[i]);
cout << ans << endl;
return 0;
}
假设输入是一张有向无环图,且 。
判断题
- 如果图中没有任何边,程序输出
1。
{{ select(21) }}
- 正确
- 错误
- 数组 在读入后表示每个点的出度。
{{ select(22) }}
- 正确
- 错误
- 程序结束时, 表示以点 结尾的最长路径包含的点数。
{{ select(23) }}
- 正确
- 错误
单项选择题
- 若输入为:
5 4
1 3
2 3
3 4
2 5
{{ select(24) }}
- 2
- 3
- 4
- 5
- 若输入为:
4 4
1 2
2 3
3 4
1 3
{{ select(25) }}
- 2
- 3
- 4
- 5
- 该程序的主要计算部分时间复杂度为?
{{ select(26) }}
阅读程序(三)
#include <iostream>
using namespace std;
int n, a[20], ans;
void dfs(int p, int s) {
if (p > n) {
if (s == 0)
ans++;
return;
}
dfs(p + 1, s + a[p]);
dfs(p + 1, s - a[p]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
dfs(1, 0);
cout << ans << endl;
return 0;
}
假设 ,输入的所有 均为非负整数。
判断题
- 若所有 的和为奇数,程序一定输出
0。
{{ select(27) }}
- 正确
- 错误
- 若所有 都为 ,程序输出 。
{{ select(28) }}
- 正确
- 错误
- 该程序的时间复杂度为 。
{{ select(29) }}
- 正确
- 错误
单项选择题
- 若输入为:
3
1 1 2
{{ select(30) }}
- 0
- 2
- 3
- 4
- 若输入为:
4
1 2 3 4
{{ select(31) }}
- 0
- 2
- 4
- 6
- 若输入为:
5
1 1 1 1 2
{{ select(32) }}
- 4
- 5
- 6
- 8
三、完善程序(单选题,每题 3 分,共计 30 分)
完善程序(一)
下面程序使用快速排序将数组从小到大排序,请补全程序。
#include <iostream>
using namespace std;
int a[1005];
void qs(int l, int r) {
if (l >= r)
return;
int i = l, j = r;
int x = ①;
while (②) {
while (a[i] < x)
i++;
while (③)
j--;
if (i <= j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
④;
j--;
}
}
if (l < j)
qs(l, j);
if (i < r)
⑤;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
qs(1, n);
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
return 0;
}
- ① 处应填?
{{ select(33) }}
a[l]a[(l + r) / 2]a[r]l + r
- ② 处应填?
{{ select(34) }}
i <= ji < lj > ri == j
- ③ 处应填?
{{ select(35) }}
a[j] < xa[j] > xa[i] < xa[i] > x
- ④ 处应填?
{{ select(36) }}
i--j++i++r--
- ⑤ 处应填?
{{ select(37) }}
qs(l, i)qs(j, r)qs(i, j)qs(i, r)
完善程序(二)
下面程序求使用给定硬币面值凑出金额 所需的最少硬币数量。每种硬币可以使用任意多次。若无法凑出,输出 -1。请补全程序。
#include <iostream>
using namespace std;
const int INF = 1000000000;
int c[105], dp[1005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> c[i];
dp[0] = 0;
for (int i = 1; i <= m; i++)
dp[i] = INF;
for (int i = 1; i <= n; i++) {
for (int j = ①; j <= m; j++) {
if (②)
dp[j] = ③;
}
}
if (④)
cout << -1 << endl;
else
cout << ⑤ << endl;
return 0;
}
- ① 处应填?
{{ select(38) }}
01c[i]m
- ② 处应填?
{{ select(39) }}
dp[j] < dp[j - c[i]] + 1dp[j - c[i]] + 1 < dp[j]dp[j] == 0j < c[i]
- ③ 处应填?
{{ select(40) }}
dp[j - c[i]] + 1dp[j] + 1dp[c[i]]c[i]
- ④ 处应填?
{{ select(41) }}
dp[0] == INFdp[m] == 0dp[m] < INFdp[m] == INF
- ⑤ 处应填?
{{ select(42) }}
nmdp[m]INF