Editorial for Đoán số
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
-
Để giao tiếp với máy chấm, ta cần in ra lệnh và có XUỐNG DÒNG. Ví dụ như trong code : cout<<mid<<'\n';
-
Sau khi in ra lệnh giao tiếp với máy chấm, trong trường hợp này là cout<<mid<<'\n';. Ta cần thực hiện lệnh flush. Lệnh này có nghĩa là bắt máy chấm đọc vào câu lệnh mà người dùng nhập. Ở Free Pascal, lệnh này là flush(output). Ở C++, lệnh này là fflush(stdout). Sau khi đã flush, các bạn mới có thể đọc dữ liệu trả về của máy, trong trường hợp này là cin>>s;.
-
Hi vọng các bạn đã hiểu cách thức giao tiếp với máy.
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll l = 1, r = (ll)(2e9),mid;
while (1) {
mid = (l + r ) / 2;
cout<<mid<<'\n';
fflush(stdout);
string s;
cin>>s;
if(s=="SMALLER")
r = mid - 1;
else if (s=="BIGGER")
l = mid+1;
else break;
}
return 0;
}
- P/s: Ở trên cái đề là "HOLA" nhé, không phải "BINGO" (vì mình đã test rồi).
- Nếu có gì khó hiểu hoặc sai , mọi người cứ comment nhé.
Comments