第 1 题 · 单选题(2 分)
面向对象的编程思想主要包括( )原则。
GESP C++ 6 级 · 历届真题
完成测评后自动生成学习记录与下一步建议。
本页列出该卷的全部题干与选项,可在在线测评中作答。
面向对象的编程思想主要包括( )原则。
运行下列代码,屏幕上输出( )。
#include <iostream>
using namespace std;
class my_class {
public:
static int count;
my_class() { count++; }
~my_class() { count--; }
static void print_count() { cout << count << " "; }
};
int my_class::count = 0;
int main() {
my_class obj1;
my_class::print_count();
my_class obj2;
obj2.print_count();
my_class obj3;
obj3.print_count();
return 0;
}运行下列代码,屏幕上输出( )。
#include <iostream>
using namespace std;
class shape {
protected:
int width, height;
public:
shape(int a = 0, int b = 0) : width(a), height(b) {}
virtual int area() {
cout << "parent class area: " << endl;
return 0;
}
};
class rectangle : public shape {
public:
rectangle(int a = 0, int b = 0) : shape(a, b) {}
int area() {
cout << "rectangle area: ";
return width * height;
}
};
class triangle : public shape {
public:
triangle(int a = 0, int b = 0) : shape(a, b) {}
int area() {
cout << "triangle area: ";
return width * height / 2;
}
};
int main() {
shape *pshape;
rectangle rec(10, 7);
triangle tri(10, 5);
pshape = &rec; pshape->area();
pshape = &tri; pshape->area();
return 0;
}向一个栈顶为 hs 的链式栈中插入一个指针为 s 的结点时,应执行( )。
在栈数据结构中,元素的添加和删除是按照什么原则进行的?
要实现将一个输入的十进制正整数转化为二进制表示,下面横线上应填入的代码为( )。
#include <iostream>
#include <stack>
using namespace std;
stack<int> ten2bin(int n) {
stack<int> st;
while (n > 0) {
st.push(n % 2);
n /= 2;
}
return st;
}
int main() {
int n;
cin >> n;
stack<int> bin = ten2bin(n);
while (!bin.empty()) {
// 在此处填入代码
________
}
return 0;
}下面定义了一个循环队列的类,请补全判断队列是否满的函数,横线上应填写( )。
class circular_queue {
private:
int *arr;
int capacity;
int front;
int rear;
public:
circular_queue(int size) {
capacity = size + 1;
arr = new int[capacity];
front = rear = 0;
}
bool is_empty() { return front == rear; }
bool is_full() {
// 在此处填入代码
________
}
void en_queue(int data) {
if (is_full()) return;
arr[rear] = data;
rear = (rear + 1) % capacity;
}
};对字符串 classmycls 使用哈夫曼(Huffman)编码,最少需要( )比特。
二叉树的( )第一个访问的节点是根节点。
一棵 5 层的满二叉树中节点数为( )。
在求解最优化问题时,动态规划常常涉及到两个重要性质,即最优子结构和( )。
青蛙每次能跳 1 或 2 步,下面代码计算青蛙跳到第 n 步台阶有多少种不同跳法。下列说法错误的是( )。
int jump_recur(int n) {
if (n <= 2) return n;
return jump_recur(n - 1) + jump_recur(n - 2);
}
int jump_dp(int n) {
if (n <= 2) return n;
vector<int> dp(n + 1);
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; ++i)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}阅读以下二叉树的广度优先搜索代码。二叉树结构:根节点 5;5 的左、右孩子分别为 2、17;2 的左、右孩子分别为 -4、3;17 的左孩子为 9。 搜索数值 20 时,可能的输出是( )。
TreeNode* bfs(TreeNode* root, int target) {
if (root == nullptr) return nullptr;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* node = q.front(); q.pop();
if (node->val == target) return node;
cout << node->val << " ";
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
return nullptr;
}在上一题的二叉树上,使用以下非递归深度优先搜索代码搜索数值 20,可能的输出是( )。
TreeNode* dfs(TreeNode* root, int target) {
if (root == nullptr) return nullptr;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); st.pop();
if (node->val == target) return node;
cout << node->val << " ";
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
return nullptr;
}在前两题的二叉树中搜索数值 3,采用深度优先搜索一共比较的节点数为( )。
TreeNode* dfs(TreeNode* root, int target) {
if (root == nullptr) return nullptr;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); st.pop();
if (node->val == target) return node;
cout << node->val << " ";
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
return nullptr;
}哈夫曼编码本质上是一种贪心策略。
创建一个对象时,会自动调用该对象所属类的构造函数。如果没有定义构造函数,编译器会自动生成一个默认的构造函数。
定义一个类时,必须手动定义一个析构函数,用于释放对象所占用的资源。
C++ 中类内部可以嵌套定义类。
000, 001, 011, 010, 110, 111, 101, 100 是一组格雷码。
n 个节点的双向循环链表中,查找某个节点的平均时间复杂度是 O(log n)。
完全二叉树可以用数组存储数据。
在 C++ 中,静态成员函数不能直接访问非静态成员变量。
在深度优先搜索中,通常使用队列来辅助实现。
对 0-1 背包问题,贪心算法一定能获得最优解。
试题名称:计算得分
小杨想要计算由 m 个小写字母组成的字符串的得分。他设置了一个包含 n 个正整数的计分序列 A=(a_1,a_2,…,a_n)。如果字符串的一个子串由 k(1≤k≤n)个 abc 首尾相接组成,那么能够得到分数 a_k;字符串中的字符不能重复计分。整个字符串得分是所选计分子串分数之和。小杨想知道给定字符串的最大总得分。
例如,计分序列为 (3,1,2),字符串为 dabcabcabcabz。可以把三个 abc 分别计分,得分为 3+3+3=9。
输入格式:第一行一个正整数 n,表示计分序列长度;第二行 n 个正整数 a_1…a_n;第三行一个正整数 m,表示字符串长度;第四行一个长度为 m 的小写字母串。
输出格式:输出最大总得分。
样例输入
3
3 1 2
13
dabcabcabcabz样例输出
9数据范围:1≤n≤20,1≤m≤10^5,1≤a_i≤1000。时间限制 1 秒,内存限制 512 MB。
本题为编程题,可在在线测评中提交 C++ 代码,由系统按测试点自动评测。
试题名称:二叉树
小杨有一棵包含 n 个节点的二叉树,根节点编号为 1。每个节点为白色或黑色。每次操作选择一个节点,将以该节点为根的子树内所有节点颜色反转(黑变白、白变黑)。请输出 q 次操作全部完成后的每个节点颜色。
输入格式:第一行一个正整数 n;第二行 n−1 个正整数,第 i 个数表示节点 i+1 的父节点编号;第三行长度为 n 的 01 串,0 表示白色、1 表示黑色;第四行一个正整数 q;接下来 q 行每行一个节点编号 x,表示翻转以 x 为根的子树。
输出格式:输出长度为 n 的 01 串,依次表示节点 1…n 的最终颜色。
样例输入
6
3 1 1 3 4
100101
3
1
3
2样例输出
010000数据范围:1≤n,q≤10^5。输入给定的父节点关系保证构成一棵二叉树。时间限制 1 秒,内存限制 512 MB。
本题为编程题,可在在线测评中提交 C++ 代码,由系统按测试点自动评测。
登录后可以开始测评,完成后查看学习报告和下一步训练建议。