注意: 雖然範圍是 ,但用這範圍如果出題人的數是最後一個,那最後一次會還沒有猜就退出循環,所以保險點直接用
#include <iostream>
using namespace std;
int main() {
int T;
cin >> T;
int flag = 0;
while (T--) {
int l, r, n;
cin >> l >> r >> n;
l ++;
while (true) {
int mid = l + r >> 1;
cout << mid << endl;
string s;
cin >> s;
if (s == "CORRECT") break;
if (s == "TOO_SMALL")
l = mid + 1;
else
r = mid;
}
}
}
eoj 3342. 經典的猜數字遊戲
來源:https://acm.ecnu.edu.cn/problem/3342/
#include <iostream>
using namespace std;
int main() {
int l = -1e9, r = 1e9;
while (l < r) {
int mid = l + r >> 1;
cout << mid << endl; // 交互題。先猜,把mid輸出到標準輸出流。endl 清理緩存區
string s;
cin >> s; // 對於我的回答,你要從標準輸入流中讀入
if (s == "equal") break;
if (s == "big") r = mid;
else l = mid + 1;
}
return 0;
}