EXY-SC-1265
第 236 题
下列 C++ 代码判断一个正整数是否是质数,说法正确的是( )。
bool is_prime(int n) {
if (n <= 1)
return false;
if (n == 2 || n == 3 || n == 5)
return true;
if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0)
return false;
int i = 7;
int step = 4;
int finish_number = sqrt(n) + 1;
while (i <= finish_number) {
if (n % i == 0)
return false;
i += step;
step = 6 - step;
}
return true;
}
语言:
C++
GESP真题
五级
2025.6
单选题号:
5
EXY-SC-1264
第 237 题
下列 C++ 代码用循环链表解决约瑟夫问题,即假设 $n$ 个人围成一圈,从第一个人开始数,每次数到第 $k$ 个的人就出圈,输出最后留下的那个人的编号。横线上应填写( )。
struct Node {
int data;
Node* next;
};
Node* createCircularList(int n) {
Node* head = new Node{1, nullptr};
Node* prev = head;
for (int i = 2; i <= n; ++i) {
Node* node = new Node{i, nullptr};
prev->next = node;
prev = node;
}
prev->next = head;
return head;
}
int findLastSurvival(int n, int k) {
Node* head = createCircularList(n);
Node* p = head;
Node* prev = nullptr;
while (p->next != p) {
for (int count = 1; count < k; ++count) {
prev = p;
p = p->next;
}
_____________________ // 横线处
}
cout << "最后留下的人编号是:" << p->data << endl;
delete p;
return 0;
}
语言:
C++
GESP真题
五级
2025.6
单选题号:
4
EXY-SC-1263
第 238 题
基于上题代码正确的前提下,填入相应代码完善 append(),用于在双向链表尾部增加新节点,横线上应填写( )。
void append(int data) {
Node* newNode = new Node{data, nullptr, nullptr};
if (is_empty()) {
head = tail = newNode;
} else {
_________________ // 横线处
}
++size;
}
语言:
C++
GESP真题
五级
2025.6
单选题号:
3
EXY-SC-1262
第 239 题
下面 C++ 代码实现双向链表。函数 is_empty() 判断链表是否为空,如链表为空返回 true,否则返回 false。横线处不能填写( )。
// 节点结构体
struct Node {
int data;
Node* prev;
Node* next;
};
// 双向链表结构体
struct DoubleLink {
Node* head;
Node* tail;
int size;
DoubleLink() {
head = nullptr;
tail = nullptr;
size = 0;
}
~DoubleLink() {
Node* curr = head;
while (curr) {
Node* next = curr->next;
delete curr;
curr = next;
}
}
// 判断链表是否为空
bool is_empty() const {
_______________________
}
};
语言:
C++
GESP真题
五级
2025.6
单选题号:
2
EXY-SC-1261
第 240 题
与数组相比,链表在( )操作上通常具有更高的效率。
语言:
C++
GESP真题
五级
2025.6
单选题号:
1
当前页显示 236 - 240
,共 1260 道单选题