题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
ips=list(map(int,input().split("."))) ip_ten=int(input()) ip_binstr="" for ip in ips: ip=str(bin(ip)[2:]) while len(ip)<8: ip="0"+ip ip_binstr+=ip print(int(ip_binstr,2)) ip_binstr=str(bin(ip_ten)[2:]) ip_binlist=[] while len(ip_binstr)<32: ip_binstr="0"+ip_binstr ip_binlisttmp=list(ip_binstr) tmps="" for i in range(len(ip_binstr)): tmps+=str(ip_binlisttmp[i]) if i%8==7: ip_binlist.append(tmps) tmps="" for i in range(len(ip_binlist)): print(str(int(ip_binlist[i],2)),end="") if i+1!=len(ip_binlist): print(".",end="")