#CSPSC2021. 2021 CPS-S

2021 CPS-S

2021年CCF非专业级别软件能力认证第一轮 (CSP-S)提高级C++语言试题

题目总数:20 总分数:100 一、单项选择题((共1515题,每题22分,共计3030分:每题有且仅有一个正确选项))

11. 在 Linux 系统终端中,用于列出当前目录下所含的文件和子目录的命令为( ) {{ select(1) }}

  • ls\text{ls}
  • cd\text{cd}
  • cp\text{cp}
  • all\text{all}

22. 二进制数 101121011_2110121101_2 的和为( )。 {{ select(2) }}

  • 00111100200111100_2
  • 01000000201000000_2
  • 00111100200111100_2
  • 01000010201000010_2

33. 在程序运行过程中,如果递归调用的层数过多,可能会由于( )引发错误。 {{ select(3) }}

  • 系统分配的栈空间溢出
  • 系统分配的队列空间溢出
  • 系统分配的链表空间溢出
  • 系统分配的堆空间溢出

44. 以下排序方法中,( )是不稳定的。 {{ select(4) }}

  • 插入排序
  • 冒泡排序
  • 堆排序
  • 归并排序

55. 以比较为基本运算,对于 2n2n 个数,同时找到最大值和最小值,最坏情况下需要的最小的比较次数为( )。 {{ select(5) }}

  • 4n24n - 2
  • 3n+13n + 1
  • 3n23n - 2
  • 2n+12n + 1

66. 现有一个地址区间为 001010 的哈希表,对于出现冲突情况,会往后找第一个空的地址存储(到 1010 冲突了就从 00 开始往后),现在要依次存储(0011223344556677),哈希函数为 h(x)=x2mod11h(x)=x^2 \bmod 11。请问 77 存储在哈希表哪个地址中( )。 {{ select(6) }}

  • 55
  • 66
  • 77
  • 88

77. GG 是一个非连通简单无向图(没有自环和重边),共有 3636 条边,则该图至少有( )个点。 {{ select(7) }}

  • 88
  • 99
  • 1010
  • 1111

88. 令根结点的高度为 11,则一棵含有 20212021 个结点的二叉树的高度至少为( )。 {{ select(8) }}

  • 1010
  • 1111
  • 1212
  • 20212021

99. 前序遍历和中序遍历相同的二叉树为且仅为( )。 {{ select(9) }}

  • 只有 11 个点的二叉树
  • 根结点没有左子树的二叉树
  • 非叶子结点只有左子树的二叉树
  • 非叶子结点只有右子树的二叉树

1010. 定义一种字符串操作为交换相邻两个字符。将“DACFEB”变为 “ABCDEF”最少需要( )次上述操作。 {{ select(10) }}

  • 77
  • 88
  • 99
  • 66

1111. 有如下递归代码:

solve(t, n):
  if t=1 return 1
  else return 5*solve(t-1,n) mod n

solve(23,23)\text{solve}(23,23) 的结果为( )。 {{ select(11) }}

  • 11
  • 77
  • 1212
  • 2222

1212. 斐波那契数列的定义为:F1=1F_1 = 1F2=1F_2 = 1Fn=Fn1+Fn2F_n = F_{n - 1}+F_{n - 2} (n3n\geq3)。现在用如下程序来计算斐波那契数列的第 nn 项,其时间复杂度为( )。

F(n):
  if n<=2 return 1
  else return F(n-1) + F(n-2)

{{ select(12) }}

  • O(n)O(n)
  • O(n2)O(n^2)
  • O(2n)O(2^n)
  • O(nlogn)O(n \log n)

1313. 有 88 个苹果从左到右排成一排,你要从中挑选至少一个苹果,并且不能同时挑选相邻的两个苹果,一共有( )种方案。 {{ select(13) }}

  • 3636
  • 4848
  • 5454
  • 6464

1414. 设一个三位数 abcabcaabbcc 均为 1199 之间的整数,若以 aabbcc 作为三角形的三条边可以构成等腰三角形(包括等边),则这样的 nn 有( )个。 {{ select(14) }}

  • 8181
  • 120120
  • 165165
  • 216216

1515. 有如下的有向图,节点为 AABB,…,JJ,其中每条边的长度都标在图中。则节点 AA 到节点 JJ 的最短路径长度为( )。

{{ select(15) }}

  • 1616
  • 1919
  • 2020
  • 2222

二、阅读程序(程序输入不超过数组或字符串定义的范围;判断题正确填√,错误填×;除特殊说明外,判断题1.5分,选择题3分,共计40分)

阅读下列程序完成16-21题

01 #include <iostream>
02 #include <cmath>
03 using namespace std;
04 const double r = acos(0.5);
05 int a1, b1, c1, d1;
06 int a2, b2, c2, d2;
07 inline int sq(const int x) { return x * x; }
08 inline int cu(const int x) { return x * x * x; }
09 int main() {
10     cout.flags(ios::fixed);
11     cout.precision(4);
12     cin >> a1 >> b1 >> c1 >> d1;
13     cin >> a2 >> b2 >> c2 >> d2;
14     int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
15     if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
16     else if (t >= sq(d2 + d1)) cout << 0;
17     else {
18         double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
19         double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
20         cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
21     }
22     cout << endl;
23     return 0;
24 }

假设输入的所有数的绝对值都不超过 1000,完成下面的判断题和单选题:

1616. 将第 2121 行中 tt 的类型声明从 int 改为 double,不会影响程序运行的结果。 {{ select(16) }}

  • 正确
  • 错误

1717. 将第 26262727 行中的 "/ sqrt(t) / 2" 替换为 "/ 2 / sqrt(t)",不会影响程序运行的结果。 {{ select(17) }}

  • 正确
  • 错误

1818. 将第 2828 行中的 "x * x" 改成 "sq(x)""y * y" 改成 "sq(y)",不会影响程序运行的结果。 {{ select(18) }}

  • 正确
  • 错误

1919. 当输入为 "0 0 0 1 1 0 0 1" 时,输出为 "1.3090"。 {{ select(19) }}

  • 正确
  • 错误

2020. 当输入为 "1 1 1 1 1 1 1 2" 时,输出为( )。 {{ select(20) }}

  • "3.1416"
  • "6.2832"
  • "4.7124"
  • "4.1888"

2121. 这段代码的含义为( )。 {{ select(21) }}

  • 求圆的面积并
  • 求球的体积并
  • 求球的体积交
  • 求椭球的体积并

阅读下列程序完成22-27题

01 #include <algorithm>
02 #include <iostream>
03 using namespace std;
04 int n, a[1005];
05 struct Node {
06     int h, j, m, w;
07     Node(const int _h, const int _j, const int _m, const int _w) : h(_h), j(_j), m(_m), w(_w) {}
08     Node operator+(const Node &o) const {
09         return Node(
10             max(h, w + o.h),
11             max(max(j, o.j), m + o.h),
12             max(m + o.w, o.m),
13             w + o.w);
14     }
15 };
16 Node solve1(int h, int m) {
17     if (h > m)
18         return Node(-1, -1, -1, -1);
19     if (h == m)
20         return Node(max(a[h], 0), max(a[h], 0), max(a[h], 0), a[h]);
21     int j = (h + m) >> 1;
22     return solve1(h, j) + solve1(j + 1, m);
23 }
24 int solve2(int h, int m) {
25     if (h > m)
26         return -1;
27     if (h == m)
28         return max(a[h], 0);
29     int j = (h + m) >> 1;
30     int wh = 0, wm = 0;
31     int wht = 0, wmt = 0;
32     for (int i = j; i >= h; i--) {
33         wht += a[i];
34         wh = max(wh, wht);
35     }
36     for (int i = j + 1; i <= m; i++) {
37         wmt += a[i];
38         wm = max(wm, wmt);
39     }
40     return max(max(solve2(h, j), solve2(j + 1, m)), wh + wm);
41 }
42 int main() {
43     cin >> n;
44     for (int i = 1; i <= n; i++) cin >> a[i];
45     cout << solve1(1, n).j << endl;
46     cout << solve2(1, n) << endl;
47     return 0;
48 }

假设输入的所有数的绝对值都不超过 1000,完成下面的判断题和单选题:

2222. 程序总是会正常执行并输出两行两个相等的数。 {{ select(22) }}

  • 正确
  • 错误

2323. 第 2828 行与第 3838 行分别有可能执行两次及以上。 {{ select(23) }}

  • 正确
  • 错误

2424. 当输入为 "5 -10 11 -9 5 -7" 时,输出的第二行为 "7"。 {{ select(24) }}

  • 正确
  • 错误

2525. solve1(1,n)\text{solve1}(1, n) 的时间复杂度为( )。 {{ select(25) }}

  • Θ(logn)\Theta(\log n)
  • Θ(n)\Theta(n)
  • Θ(nlogn)\Theta(n \log n)
  • Θ(n2)\Theta(n^2)

2626. solve2(1,n)\text{solve2}(1, n) 的时间复杂度为( )。 {{ select(26) }}

  • Θ(logn)\Theta(\log n)
  • Θ(n)\Theta(n)
  • Θ(nlogn)\Theta(n \log n)
  • Θ(n2)\Theta(n^2)

2727. 当输入为 "10 -3 2 10 0 -8 9 -4 -5 9 4" 时,输出的第一行为( )。 {{ select(27) }}

  • "13"
  • "17"
  • "24"
  • "12"

阅读下列程序完成28-33题

01 #include <iostream>
02 #include <cmath>
03 using namespace std;
04 const double r = acos(0.5);
05 int a1, b1, c1, d1;
06 int a2, b2, c2, d2;
07 inline int sq(const int x) { return x * x; }
08 inline int cu(const int x) { return x * x * x; }
09 int main() {
10     cout.flags(ios::fixed);
11     cout.precision(4);
12     cin >> a1 >> b1 >> c1 >> d1;
13     cin >> a2 >> b2 >> c2 >> d2;
14     int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
15     if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
16     else if (t >= sq(d2 + d1)) cout << 0;
17     else {
18         double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
19         double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
20         cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
21     }
22     cout << endl;
23     return 0;
24 }

假设输入总是合法的(一个整数和一个不含空白字符的字符串,用空格隔开),完成下面的判断题和单选题:

2828. 程序总是先输出一行一个整数,再输出一行一个字符串。 {{ select(28) }}

  • 正确
  • 错误

2929. 对于任意不含空白字符的字符串 str1,先执行程序输入 "0 str1",得到输出的第二行记为 str2;再执行程序输入 "1 str2",输出的第二行必为 str1。 {{ select(29) }}

  • 正确
  • 错误

3030. 当输入为 "1 SGVsbG93b3JsZA==" 时,输出的第二行为 "HelloWorld"。 {{ select(30) }}

  • 正确
  • 错误

3131. 设输入字符串长度为 nnencode 函数的时间复杂度为( )。 {{ select(31) }}

  • Θ(n)\Theta(\sqrt{n})
  • Θ(n)\Theta(n)
  • Θ(nlogn)\Theta(n \log n)
  • Θ(n2)\Theta(n^2)

3232. 输出的第一行为( )。 {{ select(32) }}

  • "0xff"
  • "255"
  • "0xFF"
  • "-1"

3333. 当输入为 "0 CSP2021csp" 时,输出的第二行为( )。 {{ select(33) }}

  • "Q1NQMjAyMWNzcAv="
  • "Q1NQMjAyMGNzcA=="
  • "Q1NQMjAyMGNzcAv="
  • "Q1NQMjAyMWNzcA=="

三、完善程序(单选题,每小题 33 分,共计 3030 分)

(1)(魔法数字问题)小H的魔法数字是 4\text{4} 。给定整数 nn ,需通过若干个 4\text{4} 进行加法、减法和整除运算得到 nn 。由于小H计算能力有限,计算过程中出现的数都不能超过 M=10000M = \text{10000} 且必须为正整数。求得到 nn 至少需要用到多少个 4\text{4}

例如,当 n=2n = \text{2} 时,有 n=4+44n=\frac{\text{4} + \text{4}}{\text{4}} ,用到了 3\text{3}4\text{4} ,此为最优方案。

阅读下列程序完成34-37题

01 #include <iostream>
02 #include <cstdlib>
03 #include <climits>
04
05 using namespace std;
06
07 const int M = 10000;
08 bool Vis[M + 1];
09 int F[M + 1];
10
11 void update(int &x, int y) {
12     if (y < x)
13         x = y;
14 }
15
16 int main() {
17     int n;
18     cin >> n;
19     for (int i = 0; i <= M; i++)
20         F[i] = INT_MAX;
21     ①;
22     int r = 0;
23     while (②) {
24         r++;
25         int x = 0;
26         for (int i = 1; i <= M; i++)
27             if (③)
28                 x = i;
29         Vis[x] = 1;
30         for (int i = 1; i <= M; i++)
31             if (④) {
32                 int t = F[i] + F[x];
33                 if (i + x <= M)
34                     update(F[i + x], t);
35                 if (i != x)
36                     update(F[abs(i - x)], t);
37                 if (i % x == 0)
38                     update(F[i / x], t);
39                 if (x % i == 0)
40                     update(F[x / i], t);
41             }
42     }
43     cout << F[n] << endl;
44     return 0;
45 }

3434. ①处应填( ) {{ select(34) }}

  • F[4] = 0
  • F[1] = 4
  • F[1] = 2
  • F[4] = 1

3535. ②处应填( ) {{ select(35) }}

  • !Vis[n]
  • r
  • F[M] == INT_MAX
  • F[n] == INT_MAX

3636. ③处应填( ) {{ select(36) }}

  • F[i] == r
  • !Vis[i] && F[i] == r
  • F[i] < r
  • !Vis[i] && F[i] < F[x]

3737. ④处应填( ) {{ select(37) }}

  • F[i] < F[x]
  • F[i]<=r
  • Vis[i]
  • i < x

(2)(2)(RMQ区间最值问题)给定序列 (a_0, \cdots, a_{n - 1}),(m) 次询问,每次询问给定 (l, r),求 (\max{a_l, \cdots, a_r})。

为了解决该问题,有一个算法叫 the Method of Four Russians ,其时间复杂度为 (O(n + m)),步骤如下:

  • 建立 Cartesian(笛卡尔)树,将问题转化为树上的 LCA(最近公共祖先)问题。
  • 对于 LCA 问题,可以考虑其 Euler 序(即按照 DFS 过程,经过所有点,环游回根的序列),即求 Euler 序列上两点间一个新的 RMQ 问题。
  • 注意新的问题为 ±1 RMQ,即相邻两点的深度差一定为 1。

下面解决这个 ±1 RMQ 问题,“序列”指 Euler 序列:

  • 设 (t) 为 Euler 序列长度。取 (b = \left\lceil \log_2 t \right\rceil) 将序列每 (b) 个分为一大块,使用 ST 表(倍增表)处理大块间的 RMQ 问题,复杂度 (O(\frac{t}{b} \log t)=O(n))。
  • (重点)对于一个块内的 RMQ 问题,也需要 (O(1)) 的算法。由于差分数组 (2^{b - 1}) 种,可以预处理出所有情况下的最值位置,预处理复杂度 (O(b2^b)),不超过 (O(n))。
  • 最终,对于一个查询,可以转化为中间整的大块的 RMQ 问题,以及两端块内的 RMQ 问题。

试补全程序。 阅读下列程序完成34-37题

001 #include <iostream>
002 #include <cmath>
003
004 using namespace std;
005
006 const int MAXN = 100000, MAXT = MAXN << 1;
007 const int MAXL = 18, MAXB = 9, MAXC = MAXT / MAXB;
008
009 struct node {
010     int val;
011     int dep, dfn, end;
012     node *son[2]; // son[0], son[1] 分别表示左右儿子
013 } T[MAXN];
014
015 int n, t, b, c, Log2[MAXC + 1];
016 int Pos[(1 << (MAXB - 1)) + 5], Dif[MAXC + 1];
017 node *root, *A[MAX'T], *Min[MAXL][MAXC];
018
019 void build() { // 建立 Cartesian 树
020     static node *S[MAXN + 1];
021     int top = 0;
022     for (int i = 0; i < n; i++) {
023         node *p = &T[i];
024         while (①(top && S[top]->val < p->val)
025             ①;
026         if (top)
027             ②;
028         S[++top] = p;
029     }
030     root = S[1];
031 }
032
033 void DFS(node *p) { // 构建 Euler 序列
034     A[p->dfn = t++] = p;
035     for (int i = 0; i < 2; i++)
036         if (p->son[i]) {
037             p->son[i]->dep = p->dep + 1;
038             DFS(p->son[i]);
039             A[t++] = p;
040         }
041     p->end = t - 1;
042 }
043
044 node *min(node *x, node *y) {
045     return ③? x : y;
046 }
047
048 void ST_init() {
049     b = (int)(ceil(log2(t) / 2));
050     c = t / b;
051     Log2[1] = 0;
052     for (int i = 2; i <= c; i++)
053         Log2[i] = Log2[i >> 1] + 1;
054     for (int i = 0; i < c; i++)
055         Min[0][i] = A[i * b];
056     for (int j = 1; j < b; j++)
057         Min[0][i] = min(Min[0][i], A[i * b + j]);
058 }
059 for (int i = 1, l = 2; l <= c; i++, l <<= 1)
060     for (int j = 0; j + l <= c; j++)
061         Min[i][j] = min(Min[i - 1][j], Min[i - 1][j + (l >> 1)]);
062 }
063
064 void small_init() { // 块内预处理
065     for (int i = 0; i <= c; i++)
066         for (int j = 1; j < b && i * b + j < t; j++)
067             if (④)
068                 Dif[i] |= 1 << (j - 1);
069     for (int S = 0; S < (1 << (b - 1)); S++) {
070         int mx = 0, v = 0;
071         for (int i = 1; i < b; i++) {
072             ⑤;
073             if (v < mx) {
074                 mx = v;
075                 Pos[S] = i;
076             }
077         }
078     }
079 }
080
081 node *ST_query(int l, int r) {
082     int g = Log2[r - l + 1];
083     return min(Min[g][l], Min[g][r - (1 << g) + 1]);
084 }
085
086 node *small_query(int l, int r) { // 块内查询
087     int p = l / b;
088     int S = ⑥;
089     return A[l + Pos[S]];
090 }
091
092 node *query(int l, int r) {
093     if (l > r)
094         return query(r, l);
095     int pl = l / b, pr = r / b;
096     if (pl == pr) {
097         return small_query(l, r);
098     } else {
099         node *s = min(small_query(l, pl * b + b - 1),
                          small_query(pr * b, r));
100         if (pl + 1 <= pr - 1)
101             s = min(s, ST_query(pl + 1, pr - 1));
102         return s;
103     }
104 }
105
106 int main() {
107     int m;
108     cin >> n >> m;
109     for (int i = 0; i < n; i++)
110         cin >> T[i].val;
111     build();
112     DFS(root);
113     ST_init();
114     small_init();
115     while (m--) {
116         int l, r;
117         cin >> l >> r;
118         cout << query(T[l].dfn, T[r].dfn)->val << endl;
119     }
120     return 0;
121 }

3838. ①处应填( ) {{ select(38) }}

  • p->son[0] = S[top--]
  • p->son[1] = S[top--]
  • S[top--]->son[0] = p
  • S[top--]->son[1] = p

3939. ②处应填( ) {{ select(39) }}

  • p->son[0] = S[top]
  • p->son[1] = S[top]
  • S[top]->son[0] = p
  • S[top]->son[1] = p

4040. ③处应填( ) {{ select(40) }}

  • x->dep < y->dep
  • x < y
  • x->dep > y->dep
  • x->val < y->val

4141. ④处应填( ) {{ select(41) }}

  • A[i * b + j - 1] == A[i * b + j]->son[0]
  • A[i * b + j]->val < A[i * b + j - 1]->val
  • A[i * b + j] == A[i * b + j - 1]->son[1]
  • A[i * b + j]->dep < A[i * b + j - 1]->dep

4242. ⑤处应填( ) {{ select(42) }}

  • v += (S >> i & 1)? -1 : 1
  • v += (S >> i & 1)? 1 : -1
  • v += (S >> (i - 1) & 1)? 1 : -1
  • v += (S >> (i - 1) & 1)? -1 : 1

4343. ⑥处应填( ) {{ select(43) }}

  • (Dif[p] >> (r - p * b)) & ((1 << (r - 1)) - 1)
  • Dif[p]
  • (Dif[p] >> (l - p * b)) & ((1 << (r - 1)) - 1)
  • (Dif[p] >> ((p + 1) * b - r)) & ((1 << (r - l + 1)) - 1)