2025 年第 42 题完善程序贪心算法提高
精明与糊涂:④处应填
题目
完善程序:精明与糊涂(接上题)。 int N; bool query(int i, int j); int main(){ cin >> N; int candidate = 0; int count = ①; for (int i = 1; i < N; ++i) { if (②) { candidate = i; count = 1; } else { ③ { ④; } else { count++; } } } cout << ⑤ << endl; } ④处应填( )
考点拆解
Boyer-Moore 多数投票算法的思想:不断抵消,最终留下的候选者必属于多数派
把经典多数投票算法改写成需要双向验证的查询形式
易错提醒
阅读程序题要按变量变化顺序手推,不要跳步
选择题要检查单位、边界和题目中的否定词