Cod sursa(job #2750843)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 13 mai 2021 13:43:34
Problema Cbinteractiv Scor Ascuns
Compilator rs Status done
Runda Marime 0.76 kb
use std::io::{self,Write};
use std::process::exit;

fn ask(val : i32) -> bool {
  println!("? {}", val);
  io::stdout().flush().unwrap();
  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 from = 1;
  let mut to = n;
  while from < to {
    let mid = (to + from) / 2;
    if ask(mid) == true {
      to = mid;
    } else {
      from = mid + 1;
    }
  }
  println!("! {}", from);
  io::stdout().flush().unwrap();
}