跳转至

Interactor

Interactor,即交互器,用於交互題與選手程序交互。交互題的介紹見 題型介紹 - 交互題

Note

Testlib 僅支持 Codeforces 形式交互題,即兩程序交互。不支持 NOI 形式的選手編寫函數與其他函數交互。

請在閲讀下文前先閲讀 通用

Testlib 為 interactor 提供了一個特殊的流 std::fstream tout,它是一個 log 流,你可以在 interactor 中向它寫入,並在 checker 中用 ouf 讀取。

在 interactor 中,我們從 inf 讀取題目測試數據,將選手程序(和標程)的標準輸入寫入 stdout(在線),從 ouf 讀選手輸出(在線),從 ans 讀標準輸出(在線)。

如果 interactor 返回了 ok 狀態,checker(如果有的話)將接管工作,檢查答案合法性。

用法

Windows:

1
interactor.exe <Input_File> <Output_File> [<Answer_File> [<Result_File> [-appes]]],

Linux:

1
./interactor.out <Input_File> <Output_File> [<Answer_File> [<Result_File> [-appes]]],

簡單的例子

Note

interactor 隨機選擇一個 \([1,10^9]\) 範圍內的整數,你要寫一個程序來猜它,你最多可以詢問 \(50\) 次一個 \([1,10^9]\) 範圍內的整數。

interactor 將返回:

1:詢問與答案相同,你的程序應當停止詢問。

0:詢問比答案小。

2:詢問比答案大。

注意在此題中我們不需要 ans,因為我們不需要將標準輸出與其比較;而在其他題中可能需要這麼做。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
int main(int argc, char** argv) {
  registerInteraction(argc, argv);
  int n = inf.readInt();  // 選數
  cout.flush();           // 刷新緩衝區
  int left = 50;
  bool found = false;
  while (left > 0 && !found) {
    left--;
    int a = ouf.readInt(1, 1000000000);  // 詢問
    if (a < n)
      cout << 0 << endl;
    else if (a > n)
      cout << 2 << endl;
    else
      cout << 1 << endl, found = true;
    cout.flush();
  }
  if (!found) quitf(_wa, "couldn't guess the number with 50 questions");
  ouf.readEof();
  quitf(_ok, "guessed the number with %d questions!", 50 - left);
}

本文主要翻譯自 Interactors with testlib.h - Codeforcestestlib.h 的 GitHub 存儲庫為 MikeMirzayanov/testlib