题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
mask = list(map(int, input().split('.'))) ip1 = list(map(int, input().split('.'))) ip2 = list(map(int, input().split('.'))) mask_1=''.join([i[2:].zfill(8) for i in list(map(bin, mask))]) def check(line, count): line = list(map(bin, line)) line = [i[2:].zfill(8) for i in line] line = ''.join(line) for i in range(0,25,8): record[count].append(int(line[i:i+8], 2) & int(mask_1[i:i+8], 2)) if max(mask) > 255 or mask_1.rfind('1') +1 != mask_1.find('0') or min(mask) < 0: print(1) elif max(ip1) > 255 or max(ip2) > 255 or min(ip1)<0 or min(ip2)<0: print(1) else: record = [[], []] check(ip1, 0) check(ip2, 1) flag = 0 for i in range(4): if record[0][i] != record[1][i]: print(flag:=2) break if flag != 2: print(0)一早上就做了个这,求指教