题解 | #整数与IP地址间的转换#
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def to10ip(s): ip = str() #先创建字符串,方便字符串添加 for i in range(len(s)): ip += format(int(s[i]),'08b')#转8位2进制,format转化位数的用法 print(int(ip,2)) #转化为2进制整数 def to255ip(s): num2 = bin(int(s,10))[2:] #将十进制数转化为2进制 num2ip = [] num2 = '0'*(32-len(num2)) + num2 if len(num2)<32 else num2 #可能会前面缺少‘0’,需要补充 for i in range(4): b = num2[8*i:8*i+8] b = str(int(b,2)) num2ip.append(b) print('.'.join(num2ip)) s1 = input().split('.') s2 = input() to10ip(s1) to255ip(s2)