import scala.annotation.tailrec import scala.io.StdIn object Problem246 { def question(mid: Int): Int = { println("? " + mid) StdIn.readInt() } def answer(result: Int): Unit = { println("! " + result) } def proc(): Int = { @tailrec def q(min: Int, max: Int): Int = { val mid = Math.round((min + max) / 2.0).toInt if (min == max) { return mid } question(mid) match { case 1 => q(mid, max) case 0 => q(min, mid - 1) } } val min = 1 val max = Math.pow(10, 9).toInt q(min, max) } def main(args: Array[String]) { val result = proc() answer(result) } }