下面枚举法查找最大值索引程序中,横线处应该填写的是( )
#include <iostream>
using namespace std;
int main() {
int arr[] = {3, 7, 2, 9, 5};
int maxIndex = 0;
for (int i = 1; i < 5; i++) {
_______________________
{
maxIndex = i;
}
}
cout << maxIndex;
return 0;
}答案D。位运算代码补全要注意区分位运算符(&|^)和逻辑运算符(&&||)。异或(^)常用于交换值。
位运算代码补全容易出错,因为位运算结果不如算术运算直观。记住异或的自反性。
【考点】枚举法|条件判断语句|数组遍历
这是 GESP 三级第 17 章"条件判断"模块的典型真题。命题人专门卡住"枚举法找最大值"这个高频考点——绝大多数考生背下"最大值要大于当前"却死活写反条件,在循环里把 arr[i] > arr[maxIndex] 写成 arr[maxIndex] > arr[i] 的情况比比皆是。