题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
stri = input().split('.') n1 = bin(int(stri[0]))[2:] n2 = bin(int(stri[1]))[2:] n3 = bin(int(stri[2]))[2:] n4 = bin(int(stri[3]))[2:] # 转换成二进制 def func(s): res = [] if len(s)<8: for i in range(8-len(s)): res.append('0') return ''.join(res)+s #不足8位的补0 return s print(int(func(n1)+func(n2)+func(n3)+func(n4),2)) str2 = input() str2 = bin(int(str2))[2:] # print(str2) temp = [] if len(str2)<32: # 不足32位的补0 for i in range(32 - len(str2)): temp.append('0') str2 = ''.join(temp) + str2 # print(str2) print(f'{int(str2[0:8],2)}.{int(str2[8:16],2)}.{int(str2[16:24],2)}.{int(str2[24:32],2)}')