2024年9月CCF—GESP(C++四级)编程能力等级认证试卷
四级
2024
2025-05-18 13:14:40
109次
一、单选题
下面代码实现了冒泡排序函数,则横线上应填写( )。
//交换数组arr的第i个元素和第j个元素
void swap(vector<int> &arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
int bubble_sort(vector<int> &arr) {
for (int i = arr.size() - 1; i > 0; i--) {
bool flag = false; // 标志位
________________________________ { // 在此处填入代码
if (arr[j] > arr[j + 1]) {
swap(arr, i, j);
flag = true;
}
}
if (!flag)
break; // 此轮“冒泡”未交换任何元素
}
} | A. for (int j = 0; j < arr.size() - 1; j++) |
B. for (int j = arr.size() - 1; j > 0; j–) |
| C. for (int j = 0; j < i; j++) |
D. for (int j = i-1; j <=0; j–) |
【知识点】 CCF—GESP C++四级
下面代码实现了插入排序函数(升序),则横线上应填写( )。
void insertion_sort(vector<int> &nums) {
for (int i = 1; i < nums.size(); i++) {
int base = nums[i], j = i - 1;
________________________________ { // 在此处填入代码
nums[j + 1] = nums[j];
j--;
}
nums[j + 1] = base;
}
} | A. while (j >= 0 && nums[j] > base) |
B. while (j > 0 && nums[j] > base) |
| C. while (j >= 0 && nums[j] < base) |
D. while (j > 0 && nums[j] < base) |
【知识点】 CCF—GESP C++四级
下面代码采用递推算法来实现整数 n 的阶乘 (ni=n×(n−1)×...×2×1),则横线上应填写( )。
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
________________________________ // 在此处填入代码
}
return result;
} | A. result *= i; |
B. result += i; |
| C. result *= result; |
D. result += result; |
【知识点】 CCF—GESP C++四级
小杨用文件重定向实现在 log.txt 文件中输出日志,则下面横线上应填写( )。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream log_file("log.txt");
streambuf* original_cout = cout.rdbuf();
cout.rdbuf(log_file.rdbuf());
___________________________________ // 在此处填入代码
cout.rdbuf(original_cout); // 恢复原始的标准输出缓冲区
return 0;
} | A. cout << “This output will go to the log file.” << endl; |
B. log_file << “This output will go to the log file.” << endl; |
| C. cout >> “This output will go to the log file.” >> endl; |
D. log_file >> “This output will go to the log file.” >> endl; |
【知识点】 CCF—GESP C++四级
在 C++ 中,( )正确定义一个名为 student 的结构体,其中包含一个 name 字符数组和一个 age 整数?
| A. struct student { char name[20]; int age; }; |
B. student struct { char name[20]; int age; }; |
| C. student struct { string name; int age; }; |
D. struct student { char[20] name; int age; }; |
【知识点】 CCF—GESP C++四级
运行以下代码,屏幕上将输出( )。
#include <iostream>
using namespace std;
int var = 100;
void function() {
int var = 200;
cout << var << " ";
cout << ::var << " ";
}
int main() {
cout << var << " ";
function();
var += 100;
cout << var << " ";
return 0;
} | A. 100 200 100 200 |
B. 100 200 100 300 |
| C. 100 200 200 200 |
D. 100 200 200 300 |
【知识点】 CCF—GESP C++四级
运行下面的代码,屏幕上将输出( )。
#include <iostream>
using namespace std;
int divide(int a, int b) {
if (b == 0) {
throw runtime_error("division by zero error ");
}
return a / b;
}
int main() {
int x = 10;
int y = 0; // 设为 0 会导致除零错误
try {
int result = divide(x, y);
cout << "result: " << result << endl;
} catch (const runtime_error& e) {
cout << "caught an exception: " << e.what() << endl;
}
return 0;
} | A. division by zero error result: caught an exception: |
B. result: caught an exception: division by zero error |
| C. caught an exception: division by zero error |
D. division by zero error caught an exception: division by zero error |
【知识点】 CCF—GESP C++四级
二、判断题
以下代码用递推法求斐波那契数列的第n 项,时间复杂度为指数级。
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int f0 = 0; // F(0)
int f1 = 1; // F(1)
int current;
for (int i = 2; i <= n; i++) {
current = f0+ f1; // F(n) = F(n-1) + F(n-2)
f0 = f1;
f1 = current;
}
return current;
} | A.正确 | B.错误 |
【知识点】 CCF—GESP C++四级
三、编程题
区间排序
时间限制:1.0 s
内存限制:512.0 MB
题面描述
小杨有一个包含n个正整数的序列a。
小杨计划对序列进行多次升序排序,每次升序排序小杨会选择一个区间[l,r](l≤r) 并对区间内所有数字,即al,al+1,...,ar进行升序排序。每次升序排序会在上一次升序排序的结果上进行。
小杨想请你计算出多次升序排序后的序列。
输入格式
第一行包含一个正整数n,含义如题面所示。
第二行包含n个正整数 a1,a2,...,an,代表序列。
第三行包含一个正整数q,代表排序次数。
之后q行,每行包含两个正整数li,ri,代表将区间[li,ri] 内所有数字进行升序排序。 ,
输出格式
输出一行包含n个正整数,代表多次升序排序后的序列。
输入样例
5 3 4 5 2 1 3 4 5 3 4 1 3
输出样例
1 3 4 5 2
第一次升序排序后,序列为[3,4,5,1,2];
第二次升序排序后,序列为[3,4,1,5,2];
第三次升序排序后,序列为[1,3,4,5,2];
对于全部数据,保证有1≤n≤100,1≤ai≤100,1≤q≤100,1≤li≤ri≤n。
【知识点】 CCF—GESP C++四级
黑白方块
时间限制:1.0 s
内存限制:512.0 MB
题面描述
小杨有一个n行m 列的网格图,其中每个格子要么是白色,要么是黑色。
小杨想知道网格图中是否存在一个满足如下条件的子矩形:
子矩形由4行4列组成;
子矩形的第1行 和第4行只包含白色格子;
对于子矩形的第2行 和第3行,只有第1个和第4个格子是白色的,其余格子都是黑色的;
请你编写程序帮助小杨判断。
输入格式
第一行包含一个正整数t,代表测试用例组数。
接下来是t组测试用例。对于每组测试用例,一共n+1 行。
第一行包含两个正整数n,m,含义如题面所示。
之后n行,每行一个长度为m的01 串,代表网格图第i行格子的颜色,如果为0,则对应格子为白色,否则为黑色。
输出格式
对于每组测试用例,如果存在,输出 Yes,否则输出 No。
输入样例
3 1 4 0110 5 5 00000 01100 01100 00001 01100 5 5 00000 01100 01110 00001 01100
输出样例
No Yes No
满足条件的子矩形形如:
0000 0110 0110 0000
对于全部数据,保证有1≤t≤10,1≤n,m≤100。
【知识点】 CCF—GESP C++四级
