题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
while True: try: mask,ip1,ip2=input().split('.'),input().split('.'),input().split('.') tag=0 for i in [mask,ip1,ip2]: if len(i)!=4: tag=1 break for j in i: if not j.isdigit: tag=1 break else: if int(j)<0 or int(j)>255: tag=1 break if tag==1: break if tag==1: print(1) continue smask='' for i in mask: smask+=bin(int(i))[2:].zfill(8) if smask.find('0')-1==smask.rfind('1'): pass else: tag=1 if tag==1: print(1) continue sip1='' for i in ip1: sip1+=bin(int(i))[2:].zfill(8) sip2='' for i in ip2: sip2+=bin(int(i))[2:].zfill(8) if int(smask,2) & int(sip1,2)==int(smask,2) & int(sip2,2): print(0) else: print(2) except: break