题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea?tpId=37&tqId=21256&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
ls=input().split('.') num=int(input()) #num-->bin_num def turn_to_bin(num): nums=[] while num!=0: nums.append(num%2) num=num//2 nums=nums[::-1] s='' for i in nums: s+=str(i) other_zero_len=8-len(s) while other_zero_len!=0: s='0'+s other_zero_len-=1 return s #str-->num 2进制字符串--》10进制数 def str_turn_to_num(s): sum=0 n=0 for i in s[::-1]: sum+=int(i)*2**n n+=1 return (sum) #ip--> num sum_str='' for item in ls: sum_str=sum_str+turn_to_bin(int(item)) sum=0 n=0 for i in sum_str[::-1]: sum+=int(i)*2**n n+=1 #num-->ip num=bin(num) num=str(num[2:]) if len(num)%8!=0: zero_len=8-len(num)%8 num=zero_len*'0'+num index=0 ip='' while index!=len(num): a='' a+=num[index] index+=1 while index%8!=0: a+=num[index] index+=1 ip+=str(str_turn_to_num(a))+'.' print(sum) print(ip[:-1])