GESP 2025年9月_C++四级试卷
从PDF导入:GESP 2025年9月_C++四级试卷
试卷题目预览
运行下面程序后变量a的值是( )。
int a = 42;
int* p = &a;
*p = *p + 1;
以下关于数组的描述中,( )是错误的。
给定如下定义的数组arr,则*(*(arr + 1) + 2)的值是( )。
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
下面这段代码会输出( )。
int add(int a, int b = 1); // 函数声明
int main() {
cout << add(2) << " " << add(2, 3);
return 0;
}
int add(int a, int b) { // 函数定义
return a + b;
}
下面这段代码会输出( )。
int x = 5;
void foo() {
int x = 10;
cout << x << " ";
}
void bar() {
cout << x << " ";
}
int main() {
foo();
bar();
}
下面程序运行的结果是( )。
void increaseA(int x) {
x++;
}
void increaseB(int* p) {
(*p)++;
}
int main() {
int a = 5;
increaseA(a);
cout << a << " ";
increaseB(&a);
cout << a;
}
关于结构体初始化,以下哪个选项中正确的是( )。
struct Point {int x,y;};
运行如下代码会输出( )。
struct Cat {
string name;
int age;
};
void birthday(Cat& c) {
c.age++;
}
int main() {
Cat kitty{"Mimi", 2};
birthday(kitty);
cout << kitty.name << " " << kitty.age;
}
关于排序算法的稳定性,以下说法错误的是( )。
下面代码试图实现选择排序,使其能对数组nums排序为升序,则横线上应分别填写( )。
void selectionSort(vector
int n = nums.size();
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if ( __________ ) { // 在此处填入代码
minIndex = j;
}
}
____________________; // 在此处填入代码
}
}
下面程序实现插入排序(升序排序),则横线上应分别填写( )。
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while ( j >= 0 && ____________________ ) { // 在此处填入代码
arr[j + 1] = arr[j];
j--;
}
____________________; // 在此处填入代码
}
}
关于插入排序的时间复杂度,下列说法正确的是( )。
小杨正在爬楼梯,需要n阶才能到达楼顶,每次可以爬1阶或2阶,求小杨有多少种不同的方法可以爬到楼顶,横线上应填写( )。
int climbStairs(int n) {
if (n <= 2) return n;
int prev2 = 1;
int prev1 = 2;
int current = 0;
for (int i = 3; i <= n; ++i) {
________________ // 在此处填入代码
}
return current;
}
假设有一个班级的成绩单,存储在一个长度为 n 的数组 scores 中,每个元素是一个学生的分数。老师想要找出 所有满足 scores[i] + scores[j] + scores[k] == 300 的三元组,其中 i < j < k。下面代码实现该功能,请问其时间复杂度是( )。
关于异常处理,以下说法错误的是( )。
以下代码能正确初始化指针。
int a = 5;
int *p = a;
执行下面C++代码将输出11。int x = 10; void f() { int x = x + 1; cout << x << endl; } int ma
int x = 10;
void f() {
int x = x + 1;
cout << x << endl;
}
int main() {
f();
}
以下C++代码合法。
struct Student {
string name;
int age;
float score;
};
Student* students = new Student[20];
执行下面C++代码将输出10。void func(int* p) { *p = 10; } int main() { int a = 5; func(&a);
void func(int* p) {
*p = 10;
}
int main() {
int a = 5;
func(&a);
cout << a << endl;
return 0;
}
下面代码将二维数组arr传递给函数f,函数内部用arr[i][j]访问元素,函数参数声明为int arr[][4]是错误的。
void f(int arr[][4], int rows) {
// 访问 arr[i][j]
}
int main() {
int arr[3][4] = { /* 初始化 */ };
f(arr, 3);
}
递推是在给定初始条件下,已知前一项(或前几项)求后一项的过程。
虽然插入排序的时间复杂度为O(n^2),但由于单元操作相对较少,因此在小数据量的排序任务中非常受欢迎。
对整数数组{4, 1, 3, 1, 5, 2}进行冒泡排序(将最大元素放到最后),执行一轮之后是{4, 1, 3, 1, 2, 5}。
以下代码只能捕获int类型异常。
int main() {
try {
throw 42;
} catch (...) {
cout << "Caught" << endl;
}
return 0;
}
以下代码将Hello写入文件data.txt。
ofstream file("data.txt");
cout<<"Hello"<< endl;
file.close();
排兵布阵
时间限制:1.0 s 内存限制:512.0 MB
作为将军,你自然需要合理地排兵布阵。地图可以视为n行m列的网格,适合排兵的网格以1标注,不适合排兵的网格以0标注。
现在你需要在地图上选择一个矩形区域排兵,这个矩形区域内不能包含不适合排兵的网格。请问可选择的矩形区域最多能包含多少网格?
第一行,两个正整数n,m,分别表示地图网格的行数与列数。 接下来n行,每行m个整数ai,j,表示各行中的网格是否适合排兵。
一行,一个整数,表示适合排兵的矩形区域包含的最大网格数。
【样例输入】 4 3 0 1 1 1 0 1 0 1 1 1 1 1 【样例输出】 4
最长连续段
时间限制:1.0 s 内存限制:512.0 MB
对于n个整数构成的数组A,如果对i都有A[i+1]=A[i]+1,那么称数组A是一个连续段。
给定一个长度为n的数组A,请求出其中最长的连续段的长度。
第一行,一个正整数n。 第二行,n个整数A[1],A[2],...,A[n]。
一行,一个整数,表示最长连续段的长度。
【样例输入】 5 1 2 3 5 6 【样例输出】 3