题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def ip_num(ip): ipl=map(int,ip.split(".")) l="" for i in ipl: a="" while 1: y=i%2 s=i//2 if s==0: a+=str(y) break a+=str(y) i=s l+=("0"*8+a[::-1])[-8:] num=0 for x,y in enumerate(l): num+=int(y)*2**(len(l)-x-1) return num def num_ip(num:int): x="" while 1: y=num%2 s=num//2 x+=str(y) if s==0: break num=s l="0"*32 l+=x[::-1] l=l[-32:] ip=[] for i in range(0,32,8): n=l[i:i+8] num=0 for x,y in enumerate(n): num+=int(y)*2**(len(n)-x-1) ip.append(str(num)) ip=".".join(ip) return ip while 1: try: ip=input() num=int(input()) print(ip_num(ip)) print(num_ip(num)) except: break