Language: C++

#include <vector>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <queue>
#include <iomanip>
using namespace std;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define INSEG(l,x,r) ((l)<=(x)&&(x)<(r))
#define dump(...)
#define pb push_back
#define _ 0
const signed INF_=1001001001; const long long INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;}
template <class T> void PSUM(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct before_main_function {
    before_main_function() {
        cin.tie(0); ios::sync_with_stdio(false);
        cout << setprecision(15) << fixed;
        // #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

bool ask(int n) {
    cout << "? " << n << endl;
    char resp;
    cin >> resp;
    return resp == 'Y';
}
void answer(int n) {
    cout << "! " << n << endl;
}
signed main() {
    int num9 = 9999999999ll;
    int dig = 10;
    REP(i, 10) {
        if (!ask(num9)) {
            break;
        }
        dig--;
        num9 = num9 / 10;
    }
    dig++;

    int num1 = 1000000000ll;
    int dig2 = 10;
    REP(i, 10) {
        if (ask(num1)) {
            break;
        }
        dig2--;
        num1 /= 10;
    }
    
    dig = max(dig, dig2);
    cerr << "dig = " << dig << endl;
 
    if (dig == 1) {
        REP(i, 9) {
          int tmp = (i + 1) * 10;
            if (ask(tmp)) {
                answer(i + 1);
                return 0;
            }
        }
    } else {
        int L = 1, R = 1;
        REP(i, dig - 1) R *= 10;
        L = R / 10;
        while (R - L > 1) {
            int M = (L + R) / 2;
            if (ask(M)) {
                L = M;
            } else {
                R = M;
            }
        }
        REP(i, 10) {
            int tmp = L * 100 + i * 10 + 0;
            if (ask(tmp)) {
                answer(tmp / 10);
                return 0;
            }
        }
        assert(false);
    }
    return (0^_^0);
}

Compiler output:
a.cc: In function 'int main()':
a.cc:109:9: error: 'assert' was not declared in this scope
  109 |         assert(false);
      |         ^~~~~~
a.cc:8:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    7 | #include <iomanip>
  +++ |+#include <cassert>
    8 | using namespace std;