题解 | #HJ33 整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
Python 版本
def ip2num(ip):
temp = ip.split('.')
num = 0
num = sum([int(s)*(256**(len(temp)-i-1)) for i,s in enumerate(temp)])
return num
def num2ip(num):
num = int(num)
temp = []
i = 3
while (i >= 0):
quo, num = divmod(num, 256**i)
temp.append(quo)
i -= 1
ip = '.'.join([str(i) for i in temp])
return ip
while True:
try:
ip = input()
num = input()
print(ip2num(ip))
print(num2ip(num))
except Exception:
break