#2523. 2025 CSP-J1 真题 · 完善程序(精明与糊涂)

2025 CSP-J1 真题 · 完善程序(精明与糊涂)

2025 CSP-J1 真题(120 分钟,总分 100 分)

完善程序(精明与糊涂)(共 5 题,共 15.0 分)

1. 精明与糊涂

有N个人,分为两类:

①精明人:永远能正确判断其他人是精明还是糊涂;

②糊涂人:判断不可靠,会给出随机的判断。

已知精明人严格占据多数,即如果精明人有k个,则满足 。你只能通过函数 query(i,j) 让第i 个人判断第j个人:返回true表示判断结果为“精明人”;返回false表示判断结果为“糊涂人”。目标是通过互 相判断,找出至少一个百分之百能确定的精明人(无需关心 query(i,j) 的内部实现)。

以下程序利用“精明人占多数”的优势,通过“消除”过程让人们互相判断并抵消,经过若干轮抵消后,最终

留下的候选者必然属于多数派(精明人),试补全程序。

#include <iostream>
#include <vector>
using namespace std;

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{
            if(③){
                ④;
            }else{
                count++;
            }
        }
    }
    cout << ⑤ << endl;
    return 0;
}

①处应填()。

{{ select(1) }}

  • cpp 0
  • cpp 1
  • cpp N
  • cpp -1

2. ②处应填()。

{{ select(2) }}

  • cpp count < 0
  • cpp count == 1
  • cpp count == 0
  • cpp query(candidate, i) == false

3. ③处应填()。

{{ select(3) }}

  • cpp query(candidate,i)==false
  • cpp query(i,candidate)==true
  • cpp query(candidate,i)==false && query(i,candidate)==false
  • cpp query(candidate,i)==false || query(i,candidate)==false

4. ④处应填()。

{{ select(4) }}

  • cpp count--
  • cpp break
  • cpp count++
  • cpp candidate=i

5. ⑤处应填()。

{{ select(5) }}

  • cpp N-1
  • cpp count
  • cpp candidate
  • cpp 0