题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let index = 0 rl.on('line', function (line) { index ++ solution(line) }); function solution(line: string) { if(index === 1){ let nums = line.split(".").map(x=>Number(x)) let binStr = nums.map(x=>x.toString(2).padStart(8,'0')).join("") let num = parseInt(binStr,2) console.log(num); return } if(index === 2){ let num = Number(line) let binStr = num.toString(2).padStart(32,"0") let pat = /.{8}/g let strs:string[] = [] while(true){ let res = pat.exec(binStr) if(res){ strs.push(res[0]) }else{ break } } let res = strs.map(x=>parseInt(x,2)) .map(x=>x.toString()).join(".") console.log(res); } }