题解 | #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
全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:池是池,发是发,我曾池,我现黑
点赞 评论 收藏
分享
评论
点赞
2
分享
牛客网
牛客企业服务