交互题,二分
一开始做的时候,没有思路,但是我知道用的应该是二分,但是卡在了取模操作。
做法是:用二分区间试探,每次从[0,1]、[1,2]、...[2^k,2^k+1].
在这一题上,锁定二分区间。之后,在区间中用二分法得到答案;
#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
while(cin >> s){
if(s == "end") break;
int l = 0,r = 1;
cout << "? " << l << ' ' << r << endl;
cin >> s;
while(s == "y"){
l = r,r <<= 1;
cout << "? " << l << ' ' << r << endl;
fflush(stdout);
cin >> s;
}
while(l+1 < r){
int mid = (l+r)>>1;
cout << "? " << l << ' ' << mid << endl;
fflush(stdout);
cin >> s;
if(s == "x") r = mid;
else l = mid;
}
cout << "! " << l+1 << endl;
}
}