#2374. GESP-C++-2023年12月五级真题卷GESP-C++-2023年12月五级真题卷
GESP-C++-2023年12月五级真题卷GESP-C++-2023年12月五级真题卷
GESP-C++-2023年12月五级真题卷GESP-C++-2023年12月五级真题卷
单选题 共30分
1), 下面 代码用于求斐波那契数列,该数列第 、 项为 ,以后各项均是前两项之和。下面有关说法错误的 是( )。
int fiboA(int N)
{
if(N == 1 || N==2)
return 1;
return fiboA(N-1) + fiboA(N-2);
}
int fiboB(int N)
{
if(N == 1 || N==2)
return 1;
int last2 = 1, last1 = 1;
int nowVal = 0;
for(int i=2;i<N;i++)
{
nowVal = last1 + last2;
last2 = last1;
last1 = nowVal;
}
return nowVal;
}
{{ select(1) }}
- A.
fiboA( )用递归方式,fiboB()循环方式 - B.
fiboA( )更加符合斐波那 mache 数列的数学定义,直观易于理解,而fiboB( )需要将数学定义转换为计算机程序实现 - C.
fiboA( )不仅仅更加符合数学定义,直观易于理解,且因代码量较少执行效率更高 - D.
fiboB( )虽然代码量有所增加,但其执行效率更高
2), 下面 代码以递归方式实现合并排序,并假设 merge (int T[], int R[], int s, int m, int t) 函数将有序(同样排序规则)的 和 归并到 中。横线处应填上代码是( )。
void mergeSort(int SList[], int TList[], int s,int t,int len)
{
if(s==t){
TList[s] = SList[s];
return;
}
int *T2 = new int[len];//保存中间结果
int m = (s+t)/2;
________________;
merge(T2, SList, s, m, t);
delete T2;
return ;
}
{{ select(2) }}
- A.
mergeSort(SList, T2, s, m,len), mergeSort(SList, T2, m,t,len) - B.
mergeSort(SList, T2, s, m-1,len), mergeSort(SList, T2, m+1,t,len) - C.
mergeSort(SList, T2, s, m,len), mergeSort(SList, T2, m+1,t,len) - D.
mergeSort(SList, T2, s, m-1,len), mergeSort(SList, T2, m-1,t,len)
3), 阅读下面的 代码,执行后其输出是( )。
int stepCount = 0;
int fracA(int N)
{
stepCount += 1;
cout << stepCount <<"->";
int rtn=1;
for (int i = 1; i <= N;i++)
rtn *= i;
return rtn;
}
int fracB(int N)
{
stepCount += 1;
cout << stepCount <<"->";
if(N == 1)
return 1;
return N*fracB(N-1);
}
int main()
{
cout << fracA(5);
cout << "<===>";
cout << fracB(5);
return 0;
}
{{ select(3) }}
- A. 1->120<===>2->120
- B. 1->120<===>1->120
- C. 1->120<===>1->2->3->4->5->120
- D. 1->120<===>2->3->4->5->6->120
4), 下面的 用于对 lstA 排序,使得偶数在前奇数在后,横线处应填入( )
bool isEven(int N)
{
return N % 2 == 0;
}
void swap(int &a, int &b)
{
int t;
t=a,a=b,b=t;
return;
}
void sortA(int lstA[], int n)
{
int i, j ,t;
for(i = n - 1; i > 0; i--)
for(j = 0;j < i; j++)
if(__________)
swap(lstA[j], lstA[j + 1]);
return;
}
{{ select(4) }}
- A.
!isEven(lstA[j]) && isEven(lstA[j+1]) - B.
isEven(lstA[j]) && !isEven(lstA[j+1]) - C.
lstA[j] > lstA[j+1] - D.
lstA[j] < lstA[j+1]
5), 下面的 代码用于将字符串保存到带头节点的双向链表中,并对重复的串计数,然后将最新访问的串的节点放在链头便于查找。横线处应填入代码是( )。
typedef struct Node{
string str;
int ref;
struct Node *next, *prev;
} Node;
Node * Insert(Node *pHead, string s)
{
Node *p = pHead->next;
Node *q;
while(p){
if(p->str == s){
p->ref++;
p->next->prev = p->prev;
p->prev->next = p->next;
break;
}
p=p->next;
}
if(!p){
p = new Node;
p->str = s;
p->ref = 0;
p->next = p->prev = NULL;
}
________________;
pHead->next = p, p->prev = pHead;
return pHead;
}
{{ select(5) }}
- A.
if(pHead) {p->next = pHead->next, pHead->next->prev = p;} - B.
if(pHead->next) {p->next = pHead->next, pHead->next->prev = p;} - C.
p->next = pHead->next, pHead->next->prev = p; - D. 触发异常,不能对空指针进行操作。
6), 有关下面 代码说法正确的是( )。
int rc;
int foo(int x, int y)
{
int r;
if(y == 0)
r = x;
else{
r = foo(y, x % y);
rc++;
}
return r;
}
{{ select(6) }}
- A. 如果 小于 , 值也不会超过
- B.
foo可能无限递归 - C.
foo可以求出 和 的最大公共质因子 - D.
foo能够求出 和 最小公倍数
7), 下面的 代码实现对 list 的快速排序,有关说法,错误的是( )。
vector<int> operator +(vector<int>lA,vector<int>lB)
{
vector<int>lst;
for(int i = 1; i < lA.size(); i++)
lst.push_back(lA[i]);
for(int i = 1; i < lB.size(); i++)
lst.push_back(lB[i]);
return lst;
}
vector<int>qSort(vector<int>lst)
{
if(lst.size() < 2)
return lst;
int pivot = lst[0];
vector<int>less, greater;
for(int i = 1; i < lst.size(); i++)
if(lst[i] < pivot) less.push_back(lst[i]);
else greater.push_back(lst[i]);
for(int i = 1; i < lst.size(); i++)
if(lst[i] < pivot) less.push_back(lst[i]);
else greater.push_back(lst[i]);
return ________________;
}
{{ select(7) }}
- A.
qSort(less) + qSort(greater) + (vector<int>)pivot - B.
(vector<int>)pivot + (qSort(less) + qSort(greater)) - C.
(qSort(less) + (vector<int>)pivot + qSort(greater)) - D.
qSort(less) + pivot + qSort(greater)
8), 下面 代码中的 isPrimeA() 和 isPrimeB() 都用于判断参数 是否素数,有关其时间复杂度的正确说法是( )。
bool isPrimeA(int N)
{
if(N < 2)
return false;
for(int i =2; i <= N / 2; i++)
if(n % i == 0)
return false;
return true;
}
bool isPrimeB(int N)
{
if (N < 2)
return false;
for(int i = 2; i<= sqtr(N); i++)
if(N % i == 0)
return false;
return true;
}
{{ select(8) }}
- A.
isPrimeA()的最坏时间复杂度是 ,isPrimeB()的最坏时间复杂度是 ,isPrimeA()优于isPrimeB() - B.
isPrimeA()的最坏时间复杂度是 ,isPrimeB()的最坏时间复杂度是 ,isPrimeA()优于isPrimeB() - C.
isPrimeA()的最坏时间复杂度是 ,isPrimeB()的最坏时间复杂度是 ,isPrimeA()优于isPrimeB() - D.
isPrimeA()的最坏时间复杂度是 ,isPrimeB()的最坏时间复杂度是 ,isPrimeA()优于isPrimeB()
9), 下面 代码用于有序 list 的二分查找,有关说法错误的是( )。
int _binarySearch(vector<int>lst, int Low, int High, int Target)
{
if(Low > High)
return -1;
int Mid = (Low + High) / 2;
if(Target == lst[Mid])
return Mid;
else if(Target < lst[Mid])
return _binarySearch(lst, Low, Mid - 1, Target);
else
return _binarySearch(lst, Mid + 1, High, Target);
}
int bSearch(vector<int>lst, int Val)
{
return _binarySearch(lst, 0, lst.size(), Val);
}
{{ select(9) }}
- A. 代码采用二分法实现有序
list的查找 - B. 代码采用分治算法实现有序
list的查找 - C. 代码采用递归方式实现有序
list的查找 - D. 代码采用动态规划算法实现有序
list的查找
10), 在上题的 _binarySearch 算法中,如果 lst 中有 个元素,其时间复杂度是( )。
{{ select(10) }}
- A.
- B.
- C.
- D.
11), 下面的 代码使用数组模拟整数加法,可以处理超出大整数范围的加法运算。横线处应填入代码是()。
vector<int> operator +(vector<int> a,vector<int> b)
{
vector<int> c;
int t=0;
for(int i = 1; i < a.size() || i < b.size(); i++)
{
if(i < a.size()) t = t + a[i]);
if(i < b.size()) t = t + b[i]);
________________
}
if(t) c.push_back(t);
return c;
}
{{ select(11) }}
- A.
c.push_back(t % 10), t = t % 10; - B.
c.push_back(t / 10), t = t % 10; - C.
c.push_back(t / 10), t = t / 10; - D.
c.push_back(t % 10), t = t / 10;
12), 有关下面 代码的说法正确的是( )。
class Node
{
public:
int Value;
Node* Prev;
Node* Next;
Node(int Val, Node* Prv = NULL, Node* Nxt = NULL);
};
Node::Node(int Val, Node*Prv,Node* Nxt)
{
this->Value = Val;
this->Prev = Prv;
this->Next = Nxt;
}
int main()
{
Node firstNode = Node(10);
firstNode.Next = new Node(100, &firstNode);
firstNode.Next->Next = new Node(111, firstNode.Next);
}
{{ select(12) }}
- A. 上述代码构成单向链表
- B. 上述代码构成双向链表
- C. 上述代码构成循环链表
- D. 上述代码构成指针链表
13), 通讯卫星在通信网络系统中主要起到()的作用。 {{ select(13) }}
- A. 信息过滤
- B. 信号中继
- C. 避免攻击
- D. 数据加密
14), 小杨想编写一个判断任意输入的整数 是否为素数的程序,下面哪个方法不合适?( ) {{ select(14) }}
- A. 埃氏筛法
- B. 线性筛法
- C. 二分答案
- D. 枚举法
15), 下面的排序算法都要处理多趟数据,哪种排序算法不能保证在下一趟处理时从待处理数据中选出最大或最小的数据?( ) {{ select(15) }}
- A. 选择排序
- B. 快速排序
- C. 堆排序
- D. 冒泡排序
判断题 共20分
16), 归并排序的时间复杂度是 。( ) {{ select(16) }}
- true
- false
17), 小杨在生日聚会时拿一块 的巧克力招待来的 个小朋友,保证每位小朋友至少能获得一块相同大小的巧克力。那么小杨想分出来最大边长的巧克力可以使用二分法。( ) {{ select(17) }}
- true
- false
18), 以下 代码能以递归方式实现斐波那契数列,该数列第 、 项为 ,以后各项均是前两项之和。( )
int Fibo(int N)
{
if(N == 1 || N == 2)
return 1;
else
{
int m = fiboA(N - 1);
int n = fiboB(N - 2);
return m + n;
}
}
{{ select(18) }}
- true
- false
19), 贪心算法可以达到局部最优,但可能不是全局最优解。( ) {{ select(19) }}
- true
- false
20), 小杨设计了一个拆数程序,它能够将任意的非质数自然数 转换成若干个质数的乘积,这个程序是可以设计出来的。( ) {{ select(20) }}
- true
- false
21), 插入排序有时比快速排序时间复杂度更低。( ) {{ select(21) }}
- true
- false
22), 下面的 代码能实现十进制正整数 转换为八进制并输出。( )
char s[10];
int main()
{
int N;
cin >> N;
string rst = "";
while(N != 0)
{
s[0] = N % 8 + '0';
rst += string(s);
N /= 8;
}
cout<<rst<<endl;
return 0;
}
{{ select(22) }}
- true
- false
23), 对数组 int arr[] = {2, 6, 3, 5, 4, 8, 1, 0, 9, 10} 执行 sort(arr, arr+10),则执行后 arr 中的数据调整为 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}。( )
{{ select(23) }}
- true
- false
24), 小杨想写一个程序来算出正整数 有多少个因数,经过思考他写出了一个重复没有超过 次的循环就能够算出来了。( ) {{ select(24) }}
- true
- false
25), 同样的整数序列分别保存在单链表和双向链中,这两种链表上的简单冒泡排序的复杂度相同。( ) {{ select(25) }}
- true
- false