#16518. 2023 CSP-J 初赛客观题
2023 CSP-J 初赛客观题
2023年CSP-J初赛真题
单选题(每题2分,共计30分)
- 在 C++ 中,下面哪个关键字用于声明一个变量,其值不能被修改?( ) {{ select(1) }}
unsignedconststaticmutable
- 八进制数
12345670~8~和07654321~8~ 的和为( ) {{ select(2) }}
22222221~8~21111111~8~22111111~8~22222211~8~
- 阅读下述代码,请问修改
data的value成员以存储3.14,正确的方式是( ) {{ select(3) }}
data.value = 3.14value.data = 3.14data->value = 3.14value->data = 3.14
1 union Data {
2 int num;
3 float value;
4 char symbol;
5 };
6 union Data data;
- 假设有一个链表的节点定义如下: 现在有一个指向链表头部的指针:
Node* head。如果想要在链表中插入一个新节点,其成员data的值为42,并使新节点成为链表的第一个节点,下面哪个操作是正确的? ( )
1 struct Node {
2 int data;
3 Node* next;
4 };
{{ select(4) }}
-
newNode->data = 42; newNode->next = head; head = newNode;
-
Node* newNode = new Node; head->data = 42; newNode->next = head; head = newNode;
-
Node* newNode = new Node; newNode->data = 42; head->next = newNode;
-
Node* newNode = new Node; newNode->data = 42; newNode->next = head;
- 根节点的高度为 1,一棵拥有 2023 个节点的三叉树高度至少为( ) {{ select(5) }}
- 6
- 7
- 8
- 9
- 小明在某一天中依次有七个空闲时间段,他想要选出至少一个空闲时间段来练习唱歌,但他希望任意两个练习的时间段之间都有至少两个空闲的时间段让他休息,则小明一共有( )种选择时间段的方案。( ) {{ select(6) }}
- 31
- 18
- 21
- 33
- 以下关于高精度运算的说法错误的是( ) {{ select(7) }}
- 高精度计算主要是用来处理大整数或需要保留多位小数的运算。
- 大整数除以小整数的处理的步骤可以是,将被除数和除数对齐,从左到右逐位尝试将除数乘以某个数,通过减法得到新的被除数,并累加商。
- 高精度乘法的运算时间只与参与运算的两个整数中长度较长者的位数有关。
- 高精度加法运算的关键在于逐位相加并处理进位。
- 数 和 的和为( ) {{ select(8) }}
- 后缀表达式
6 2 3 + - 3 8 2 / + * 2 ^ 3 +对应的中缀表达式是( ) {{ select(9) }}
- 假设有一组字符
{a, b, c, d, e, f},对应的频率分别为 5%, 9%, 12%, 13%, 16%, 45%。请问以下哪个选项是字符a, b, c, d, e, f分别对应的一组哈夫曼编码?( ) {{ select(10) }}
1111, 1110, 101, 100, 110, 01010, 1001, 1000, 011, 010, 00000, 001, 010, 011, 10, 111010, 1011, 110, 111, 00, 01
- 给定一棵二叉树,其前序遍历结果为:ABDECFG,中序遍历结果为:DEBACFG。请问这棵树的正确后序遍历结果是什么?( ) {{ select(11) }}
EDBGFAEDGBFCADEBGFCADBEGFCA
- 考虑一个有向无环图,该图包括 4 条有向边:
(1,2), (1,3), (2,4), (3,4)。以下哪个选项是这个有向无环图的一个有效的拓扑排序?( ) {{ select(12) }}
4, 2, 3, 11, 2, 3, 41, 2, 4, 32, 1, 3, 4
- 在计算机中,以下哪个选项描述的数据存储容量最小?( ) {{ select(13) }}
- 字节(byte)
- 比特(bit)
- 字(word)
- 千字节(kilobyte)
- 一个班级有 10 个男生和 12 个女生。如果要选出一个 3 人的小组,并且小组中必须至少包含 1 个女生,那么有多少种可能的组合?( ) {{ select(14) }}
- 1420
- 1770
- 1540
- 2200
- 以下哪个不是操作系统? ( ) {{ select(15) }}
- Linux
- Windows
- Android
- HTML
阅读程序题
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 }
假设输入的所有数都为不超过 1000 的正整数,完成下面的单选和判断题,判断题1.5分,选择题3分。
- 将第 7 行中的
(s - b) * (s - c)改为(s - c) * (s - b)不会影响程序运行的结果。( ) {{ select(16) }}
- 正确
- 错误
- 当输入为
3 4 5时,输出为( ) {{ select(17) }}
6.000012.000024.000030.0000
- 程序总是输出四位小数。( ) {{ select(18) }}
- 正确
- 错误
- 当输入为
5 12 13时,输出为( ) {{ select(19) }}
24.000030.000060.0000120.0000
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 }
- 当输入为
2 2 2时,输出为1.7321。 ---( ) {{ select(20) }}
- 正确
- 错误
f函数的返回值小于等于min(n, m)。( ) {{ select(21) }}
- 正确
- 错误
f函数的返回值等于两个输入字符串的最长公共子串的长度。( ) {{ select(22) }}
- 正确
- 错误
- 当输入两个完全相同的字符串时,
g函数的返回值总是true。( ) {{ select(23) }}
- 正确
- 错误
- 将第 19 行中的
v[m][n]替换为v[n][m],那么该程序( ) {{ select(24) }}
- 行为不变
- 只会改变输出
- 一定非正常退出
- 可能非正常退出
- 当输入为
csp-j p-jcs时,输出为( ) {{ select(25) }}
- 0
- 1
- T
- F
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 }
- 当输入为
csppsc spsccp时,输出为( ) --- 假设输入的n是绝对值不超过 1000 的整数。( ) {{ select(26) }}
- T
- F
- 0
- 1
- 如果输入的
n为正整数,solve2函数的作用是计算n所有的因子的平方和。( ) {{ select(27) }}
- 正确
- 错误
- 第 13~14 行的作用是避免
n的平方根因子i(或n / i)进入第 16 行而被计算两次。( ) {{ select(28) }}
- 正确
- 错误
- 如果输入的
n为质数p的平方,那么solve2(n)的返回值为( ) {{ select(29) }}
- 如果输入的
n为质数,solve2(n)的返回值为 。( ) {{ select(30) }}
- 正确
- 错误
- 当输入为正整数时,第一项减去第二项的差值一定( ) {{ select(31) }}
- 大于 0
- 大于等于 0 且不一定大于 0
- 小于 0
- 小于等于 0 且不一定小于 0
- 当输入为
5时,输出为( ) --- 原有长度为 、公差为 1 的等差升序数列,将数列输入到程序的数组时移除了一个元素,导致长度为 的升序数组可能不再连续,除非被移除的是第一个或最后一个元素。需要在数组不连续时,找出被移除的元素。试补全程序。( ) {{ select(32) }}
651625650729651676652625
完善程序题(每题3分,共计30分)
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 int find_missing(vector<int>& nums) {
6 int left = 0, right = nums.size() - 1;
7 while (left < right) {
8 int mid = left + (right - left) / 2;
9 if (nums[mid] == mid + ①) {
10 ②;
11 } else {
12 ③;
13 }
14 }
15 return ④;
16 }
17
18 int main() {
19 int n;
20 cin >> n;
21 vector<int> nums(n);
22 for (int i = 0; i < n; i++) cin >> nums[i];
23 int missing_number = find_missing(nums);
24 if (missing_number == ⑤) {
25 cout << "Sequence is consecutive" << endl;
26 } else {
27 cout << "Missing number is " << missing_number << endl;
28 }
29 return 0;
30 }
- ① 处应该填( ) {{ select(33) }}
- 1
nums[0]rightleft
- ② 处应该填( ) {{ select(34) }}
left = mid + 1right = mid - 1right = midleft = mid
- ③ 处应该填( ) {{ select(35) }}
left = mid + 1right = mid - 1right = midleft = mid
- ④ 处应该填( ) {{ select(36) }}
left + nums[0]right + nums[0]mid + nums[0]right + 1
- ⑤ 处应该填( ) --- 给定两个字符串,每次操作可以选择删除(Delete)、插入(Insert)、替换(Replace)一个字符,求将第一个字符串转换为第二个字符串所需要的最少操作次数。试补全动态规划算法。( ) {{ select(37) }}
nums[0] + nnums[0] + n - 1nums[0] + n + 1nums[n - 1]
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5
6 int min(int x, int y, int z) {
7 return min(min(x, y), z);
8 }
9
10 int edit_dist_dp(string str1, string str2) {
11 int m = str1.length();
12 int n = str2.length();
13 vector<vector<int>> dp(m + 1, vector<int>(n + 1));
14
15 for (int i = 0; i <= m; i++) {
16 for (int j = 0; j <= n; j++) {
17 if (i == 0)
18 dp[i][j] = ①;
19 else if (j == 0)
20 dp[i][j] = ②;
21 else if (③)
22 dp[i][j] = ④;
23 else
24 dp[i][j] = 1 + min(dp[i][j - 1], dp[i - 1][j], ⑤);
25 }
26 }
27 return dp[m][n];
28 }
29
30 int main() {
31 string str1, str2;
32 cin >> str1 >> str2;
33 cout << "Minimum number of operation:" << edit_dist_dp(str1, str2) << endl;
34 return 0;
35 }
- ① 处应填( ) {{ select(38) }}
jimn
- ② 处应填( ) {{ select(39) }}
jimn
- ③ 处应填( ) {{ select(40) }}
str1[i - 1] == str2[j - 1]str1[i] == str2[j]str1[i - 1] != str2[j - 1]str1[i] != str2[j]
- ④ 处应填( ) {{ select(41) }}
dp[i - 1][j - 1] + 1dp[i - 1][j - 1]dp[i - 1][j]dp[i][j - 1]
- ⑤ 处应填( ) {{ select(42) }}
dp[i][j] + 1dp[i - 1][j - 1] + 1dp[i - 1][j - 1]dp[i][j]