// HJ33 整数与IP地址间的转换
// 输入:10.0.3.193 167969729
// 输出:167773121 10.3.3.193
function toIp(num) { // 整数转IP
const str = Number(num).toString(2).padStart(32, "0"); // toString(2)转8位二进制*4
const arr = [
str.slice(0, 8),
str.slice(8, 16),
str.slice(16, 24),
str.slice(24, 32),
];
print(arr.map((ele) => parseInt(ele, 2)).join(".")); // parseInt('字符串', 2)将二进制转回10进制
}
function toNum(ip) { // ip转整数
const arr = ip.split(".").map((ele) => Number(ele).toString(2).padStart(8, "0")); // 每段IP都转成八位2进制
print(parseInt(arr.join(""), 2)); // 将8*4位的二进制转为10进制的数字
}
while ((line = readline())) { // 打印
if (line.includes(".")) {
toNum(line);
} else {
toIp(line);
}
}
HJ33 整数与IP地址间的转换
最新推荐文章于 2024-07-30 10:56:44 发布