题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
# def transfer(a,b): # result_a = '' # for i in a.split('.'): # a = bin(int(i,10))[2:] # result_a += '0'*(8-len(a))+a # print(int(result_a,2)) # b1 =bin(int(b))[2:] # b10 = '0'*(32-len(b1)) + b1 if len(b1)<32 else b1 # b2=[] # b3 = [] # for i in range(0,len(b10),8): # b2.append(b10[i:i+8]) # for i in b2: # b3.append(int(i,2)) # print('.'.join(map(str,b3))) # transfer(input(),input()) #第二次用到了rjust函数 def transfer(a,b): s1 = list(map(int,a.split('.'))) b1 = '' for i in s1 : b1 += bin(i)[2:].rjust(8,'0') b2 = int(b1,2) print(b2) a1 = bin(int(b,10))[2:].rjust(32,'0') ip1 = [] for i in range(4): ip1.append(str(int(a1[8*i:8*(i+1)],2))) print('.'.join(ip1)) transfer(input(),input())