2024年12月CCF—GESP(C++四级)编程能力等级认证试卷
四级
2024
2025-06-02 18:41:20
85次
一、单选题
下面的语句中,( )正确定义了一个计算浮点数 x 的平方(x2=x×x)的函数,并成功调用该函数
A.float square(float x) {
return x * x;
}
float area = square(2); |
B.square(float x) {
return x * x;
}
float area = square(2); |
C.void square(float x) {
return x * x;
}
area = square(2.0); |
D.void square(float x) {
x * x;
return;
}
area = square(2); |
【知识点】 CCF—GESP C++四级
下面代码实现了插入排序函数,则横线上应填写( )。
void insertion_sort(vector<int> &nums) {
for (int i = 1; i < nums.size(); i++) {
________________________________ { // 在此处填入代码
while (j >= 0 && nums[j] > base)
nums[j + 1] = nums[j];
j--;
}
nums[j + 1] = base;
}
} | A. int base = nums[i], j = i - 1; |
B. int base = nums[i], j = i; |
| C. int base = nums[0], j = i - 1; |
D. int base = nums[0], j = i; |
【知识点】 CCF—GESP C++四级
下面代码采用递推算法来计算斐波那契数列 f(n)=f(n−1)+f(n−2),则横线上应填写( )。
int fib(int n) {
if (n == 0 || n == 1)
return n;
int f1 = 0;
int f2 = 1;
int result = 0;
for (int i = 2; i <= n; i++) {
________________________________ // 在此处填入代码
}
return result;
} A.result = f1 + f2; f1 = f2; f2 = result; |
B.result += f1 + f2; f1 = f2; f2 = result; |
C.result += f1 + f2; f2 = result; f1 = f2; |
D.result = f1 + f2; f2 = result; f1 = f2; |
【知识点】 CCF—GESP C++四级
下面的描述中,( )不能正确定义一个名为 Student 的结构体以及一个包含20个元素的结构数组。
A.struct Student {
string name;
int age;
float score;
};
struct Student students[20]; |
B.struct Student {
string name;
int age;
float score;
};
Student students[20]; |
C.struct Student {
string name;
int age;
float score;
};
Student* students = new Student[20]; |
D.struct Student {
string name;
int age;
float score;
};
Student students = new Student[20]; |
【知识点】 CCF—GESP C++四级
下面哪种方式不能实现将字符串"Welcome to GESP!"输出重定向到文件 log.txt ( )。
A.freopen("log.txt", "w", stdout);
cout << "Welcome to GESP!" << endl;
fclose(stdout); |
B.std::ofstream outFile("log.txt");
outFile << "Welcome to GESP!" << endl;
outFile.close(); |
C.std::ofstream outFile("log.txt");
cout << "Welcome to GESP!" << endl;
outFile.close(); |
D.ofstream log_file("log.txt");
streambuf* org_cout = cout.rdbuf();
cout.rdbuf(log_file.rdbuf());
cout << "This output will go to the log file." << endl;
cout.rdbuf(oorg_cout); |
【知识点】 CCF—GESP C++四级
运行下面的代码,将出现什么情况?( )
double hmean(double a, double b) {
if (a == -b )
throw runtime_error("Runtime error occurred");
return 2.0*a*b/(a + b);
}
int main() {
double x = 10;
double y = -10;
try {
int result = hmean(x, y);
cout << "hmean: " << result << endl;
}
catch (const runtime_error& e) {
cout << "Caught: " << e.what() << endl;
} catch (...) {
cout << "Caught an unknown exception." << endl;
}
return 0;
} | A. 屏幕上输出 Caught: Runtime error occurred |
B. 屏幕上输出 Caught an unknown exception |
| C. 程序调用 std::terminate() |
D. 编译错误 |
【知识点】 CCF—GESP C++四级
二、判断题
三、编程题
Recamán
时间限制:1.0 s
内存限制:512.0 MB
题目描述
小杨最近发现了有趣的 Recamán 数列,这个数列是这样生成的:
数列的第一项 a1是 1 ;
如果 ak−1−k 是正整数并且没有在数列中出现过,那么数列的第 k 项 ak为 ak−1−k,否则为 ak−1+k。
小杨想知道 Recamán 数列的前 n 项从小到大排序后的结果。手动计算非常困难,小杨希望你能帮他解决这个问题。
输入格式
第一行,一个正整数 n 。
输出格式
一行,n 个空格分隔的整数,表示 Recamán 数列的前 n 项从小到大排序后的结果。
输入样例 1
5
输出样例 1
1 2 3 6 7
输入样例 2
8
输出样例 2
1 2 3 6 7 12 13 20
样例解释
对于样例 1,n=5 :
a1=1;
a1−2=−1,不是正整数,因此 a2=a1+2=3;
a2−3=0,不是正整数,因此 a3=a2+3=6;
a3−4=2,是正整数,且没有在数列中出现过,因此 a4=2;
a4−5=−3,不是正整数,因此 a5=a4+5=7;
a1,a2,a3,a4,a5从小到大排序后的结果为 1 2 3 6 7 。
数据范围
对于所有数据点,保证 1≤n≤3000。
【知识点】 CCF—GESP C++四级
字符排序
时间限制:1.0 s
内存限制:512.0 MB
题面描述
小杨有 n 个仅包含小写字母的字符串 s1,s2,...,sn,小杨想将这些字符串按一定顺序排列后拼接到一起构成字符串 t 。小杨希望最后构成的字符串 满足:
假设 ti为字符串 t 的第 i 个字符,对于所有的 j<i 均有 tj≤ti。两个字符的大小关系与其在字母表中的顺序一致,例如e<g<p<s。
小杨想知道是否存在满足条件的字符串排列顺序。
输入格式
第一行包含一个正整数 T TT,代表测试数据组数。
对于每组测试数据,第一行包含一个正整数 n ,含义如题面所示。
之后 n 行,每行包含一个字符串 si。
输出格式
对于每组测试数据,如果存在满足条件的排列顺序,输出 1,否则输出 0。
输入样例
3 3 aa ac de 2 aac bc 1 gesp
输出样例
1 0 0
样例解释
对于第一组测试数据,一种可行的排列顺序为 aa+ac+de,构成的字符串 t 为 aaacde,满足条件。
对于全部数据,保证有 1≤t,n≤100,每个字符串的长度不超过 10。
【知识点】 CCF—GESP C++四级
