pub fn huawei_2016_6() { fn binary_to_decimal(num: &str) -> Option<i64> { if num.len() == 0 { return None; } let mut sum = 0; let vec = num .chars() .map(|x| i64::from(x.to_digit(10).unwrap())) .collect::<Vec<i64>>(); for (index, item) in vec.iter().rev().enumerate() { sum += i64::pow(2, index as u32) * item; } Some(sum) } pub fn input_vec_num<T>(split_by: &str) -> Vec<T> where <T as std::str::FromStr>::Err: std::fmt::Debug, T: std::str::FromStr, { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); input .split(split_by) .filter(|s| !s.is_empty()) .map(|s| s.trim().to_string()) .map(|s| s.parse::<T>().unwrap()) .collect::<Vec<T>>() } pub fn input_num<T>() -> T where <T as std::str::FromStr>::Err: std::fmt::Debug, T: std::str::FromStr, { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); input.trim().to_string().parse::<T>().unwrap() } fn ip_to_int() { let ip = input_vec_num::<i64>("."); let mut ipb = String::new(); for i in ip.iter() { let s = format!("{:8b}", i).replace(" ", "0"); ipb.push_str(&s); } println!("{}", binary_to_decimal(&ipb).unwrap()); } fn int_to_ip() { let num = input_num::<i64>(); let s = format!("{:>032b}", num); let mut s_vec = vec![]; for i in (0..s.len()).step_by(8) { s_vec.push(binary_to_decimal(&s[i..i + 8]).unwrap().to_string()); } println!("{}", s_vec.join(".")); } ip_to_int(); int_to_ip(); } fn main(){ huawei_2016_6(); }
`