题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def bin_to_ten(address:list): resultto2 = [] for num in address: resultto2.append(str(bin(num)).replace('0b', '').rjust(8, '0')) result = '0b' + ''.join(resultto2) result = int(result, 2) return(result) def ten_to_bin(address:int): resultto10 = [] bin_number = str(bin(address)).replace('0b', '').rjust(32, '0') for i in range(0, len(bin_number)-7, 8): resultto10.append(bin_number[i:i+8]) for i, num in enumerate(resultto10): resultto10[i] = int('0b' + num, 2) resultto10 = list(map(str, resultto10)) result = '.'.join(resultto10) return result address2 = list(map(int, input().strip().split('.'))) address10 = int(input().strip()) print(bin_to_ten(address2)) print(ten_to_bin(address10))