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

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

def tm033():
    #读取输入
    lst=input().strip().split('.')

    Ocnum=int(input())


    #ip地址转10进制
    s=''
    for value in lst:
        x=bin(int(value))[2:]
        s=s+x.zfill(8)#字符串自动补位
    snum=int(s,2)

    #十进制ip转化为二进制ip
    s1=bin(Ocnum)[2:]
    s1=s1.zfill(32)#补位32位
    OClst=[]
    OClst.append(str(int(s1[0:8],2)))
    OClst.append(str(int(s1[8:16],2)))
    OClst.append(str(int(s1[16:24],2)))
    OClst.append(str(int(s1[24:32],2)))

    #输出
    print(snum)
    print('.'.join(OClst))

if __name__=='__main__':
    tm033()
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务