fn main() { let mut commodity: [String; 8] = [ "ジュース".to_string(), "紅茶".to_string(), "アイスティ".to_string(), "コーヒー".to_string(), "アイスコーヒー".to_string(), "ショートケーキ".to_string(), "チーズケーキ".to_string(), "トースト".to_string(), ]; let mut price: [i32; 8] = [ 280, 300, 320, 350, 370, 550, 600, 650 ]; let mut x = String::new(); std::io::stdin().read_line(&mut x).expect("Failed to read line"); let x = x.trim_end().to_owned(); let a = x.parse::<i32>().unwrap(); let n = commodity.len() as i32; bin_search(a, n, price); // seq_search(); } fn bin_search(a: i32, n: i32, price: [i32; 8]) { let mut p = -1; let mut l = 0; let mut h = n - 1; while (l <= h && p == -1) { if (a > price[((l+h) / 2) as usize]) { l = (l + h) / 2 + 1; } else { if (a == price[((l+h)/2) as usize]) { p = (l +h) / 2; } else { h = (l + h) / 2 - 1; } } } println!("{}", p); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/basic`
300
1