K. [GESP]L5-202506-Q3245-S
[GESP]L5-202506-Q3245-S
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
下面的 C++ 代码用于在升序数组 lst 中查找目标值 target 最后一次出现的位置。相关说法,正确的是( )。
int binary_search_last_occurrence(const vector<int>& lst, int target) {
if (lst.empty()) return -1;
int low = 0, high = lst.size() - 1;
while (low < high) {
int mid = (low + high + 1) / 2;
if (lst[mid] <= target) {
low = mid;
} else {
high = mid - 1;
}
}
if (lst[low] == target)
return low;
else
return -1;
}
{{ select(1) }}
- 当
lst中存在重复的target时,该函数总能返回最后一个target的位置,即便lst全由相同元素组成 - 当
target小于lst中所有元素时,该函数会返回 0 - 循环条件改为
while (low <= high)程序执行效果相同,且能提高准确性 - 将代码中
(low + high + 1) / 2修改为(low + high) / 2效果相同
GESP 2025.06 C++ 五级 全真模拟赛
- 状态
- 已结束
- 规则
- OI
- 题目
- 27
- 开始于
- 2026-11-20 13:32
- 结束于
- 2026-11-25 13:32
- 持续时间
- 2 小时
- 主持人
- 参赛人数
- 0