def query(x) STDOUT.puts("? #{x}") STDOUT.flush gets.chomp end ok = 0 ng = 1001 while (ok - ng).abs >= 2 x = (ok + ng) / 2 ret = query(x) if ret == 'safe' ok = x else ret2 = query(x + 1) if ret2 == 'safe' ok = x + 1 else ng = x end end end puts "! #{ok}"