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()
}
let ip = input_vec_num::<i64>(".");
let num = input_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());
println!(
"{}.{}.{}.{}",
((num & 0xff000000) >> 24),
((num & 0x00ff0000) >> 16),
((num & 0x0000ff00) >> 8),
((num & 0x000000ff) >> 0),
);
}
fn main(){
huawei_2016_6();
}
IPv4地址转换成整数 Rust
最新推荐文章于 2023-12-10 15:09:53 发布