题解 | #判断两个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('.'))) flag=1 for i in range(len(mask)): if mask[i] not in range(256) or ip1[i] not in range(256) or ip2[i] not in range(256): flag=0 break elif i<len(mask)-1 and mask[i]<mask[i+1]: flag=0 break else: continue a=[] b=[] for i in range(len(mask)): a.append(mask[i]&ip1[i])#python中对两个数值进行&运算,是把他们转换成2进制后再&运算 b.append(mask[i]&ip2[i]) if flag: if a==b: print('0') else: print('2') else: print('1') except: break