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

整数与IP地址间的转换

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

ips=list(map(int,input().split(".")))
ip_ten=int(input())
ip_binstr=""

for ip in ips:
    ip=str(bin(ip)[2:])
    while len(ip)<8:
        ip="0"+ip
    ip_binstr+=ip
print(int(ip_binstr,2))

ip_binstr=str(bin(ip_ten)[2:])
ip_binlist=[]

while len(ip_binstr)<32:
    ip_binstr="0"+ip_binstr
ip_binlisttmp=list(ip_binstr)
tmps=""
for i in range(len(ip_binstr)):
    tmps+=str(ip_binlisttmp[i])
    if i%8==7:
        ip_binlist.append(tmps)
        tmps=""
for i in range(len(ip_binlist)):
    print(str(int(ip_binlist[i],2)),end="")
    if i+1!=len(ip_binlist):
        print(".",end="")

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-18 14:29
牛客604067584号:感觉算法卷的人少很多,毕竟只有一部分bg还不错的硕士才会考虑算法,虽然hc不如后端,但是竞争真的少很多。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务