题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
while True: try: Mask = list(map(int, input().split('.'))) Ip1 = list(map(int, input().split('.'))) Ip2 = list(map(int, input().split('.'))) Mask_str = '' note = 0 counter = 0 for i in range(4): if Mask[i] in range(0, 256) and Ip1[i] in range(0, 256) and Ip2[i] in range(0, 256): Mask_str += bin(Mask[i])[2:].zfill(8) else: note = 1 break if Mask_str.rfind('1') + 1 != Mask_str.find('0') or note: print(1) continue for i in range(4): if (Ip1[i]) & (Mask[i]) == (Ip2[i]) & (Mask[i]): counter += 1 else: break if counter == 4: print(0) else: print(2) except: break