STDOUT.sync = true def binary_search(min, sup, count = 0) while sup - min > 1 mid = (min + sup) / 2 puts "? #{mid - count}" case gets.to_i when 0 puts "! #{mid}" exit when -1 sup = mid when 1 min = mid + 1 end count += 1 end puts "! #{min}" exit end THRESH = 100 puts "? #{THRESH}" case gets.to_i when -1 binary_search(10, THRESH, 1) when 1 binary_search(THRESH + 1, 10**9 + 1, 1) when 0 puts "! #{THRESH}" end