题解 | #字符串排序#

整数与IP地址间的转换

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

def f(): while True: try: ip = list(map(int, input().split('.'))) intip = int(input()) temp1 = '' for i in ip: temp = bin(i) temp1 += (8 - len(temp[2:])) * '0' +temp[2:] print(int(temp1, 2)) temp2 = bin(intip)[::-1] ip4 = '0b' + temp2[:8][::-1] ip3 = '0b' + temp2[8:16][::-1] ip2 = '0b' + temp2[16:24][::-1] ip1 = '0b' + temp2[24:len(temp2) - 2][::-1] res2 = str(int(ip1, 2)) + '.' + str(int(ip2,2)) + '.' + str(int(ip3,2))+'.'+str(int(ip4,2)) print(res2) except: break

f()

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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