Cod sursa(job #2750829)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 13 mai 2021 12:59:31
Problema Cbinteractiv Scor Ascuns
Compilator rs Status done
Runda Marime 0.68 kb
use std::io;
use std::process::exit;

fn ask(val : i32) -> bool {
  println!("? {}", val);
  let mut input = String::new();
  io::stdin().read_line(&mut input).expect("Failed to read line");
  let input:i32 = input.trim().parse().expect("Expected integer");
  if input == -1 {
    exit(0);
  } else { 
    return input == 1 ;
  }
}

fn main() {
  let mut n = String::new();
  io::stdin().read_line(&mut n).expect("Failed to read line");
  let n :i32 = n.trim().parse().expect("Expected integer");
  let mut x = 0;
  let mut jump = (1 << 30);
  while 0 < jump {
    if (x + jump <= n && ask(x + jump) == false) {
      x += jump;
    }
    jump /= 2;
  }
  x = x + 1;
  println!("! {}", x);
}