题解 | #整数与IP地址间的转换#

整数与IP地址间的转换

https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea

# 10.0.3.193
# 00001010 00000000 00000011 11000001
# 167773121
def ip_int(s):
   # s = list(map(int,input().split('.')))
    ss =''
    for i in s:
        ss += bin(i)[2:].zfill(8)

    return int(ss,2)
# 167969729
# 10.3.3.193
def int_ip(x):
    #x = int(input())    # 167969729
    ls = []
    xx = bin(x)[2:].zfill(32)
    for i in range(0,len(xx),8):
        ls.append(int(xx[i:i+8],2))
    lst = list(map(str,ls))
    res = ('.'.join(lst))
    return res

s = list(map(int,input().split('.')))   # 10.0.3.193
x = int(input())    # 167969729
print(ip_int(s))
print(int_ip(x))



全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:16
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 15:08
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务