题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
http://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
while True:
try:
mask = input().split(".")
ip1 = input().split(".")
ip2 = input().split(".")
if len(mask) != 4 or len(ip1) != 4 or len(ip2) != 4:
print("1")
break
for i in range(4):
mask[i] = int(mask[i])
ip1[i] = int(ip1[i])
ip2[i] = int(ip2[i])
a = mask + ip1 + ip2
if mask[0] != 255 or mask[3] != 0 or max(a)> 255 or min(a) < 0:
print("1")
else:
b = True
for i in range(4):
# 两个ip每8位和对应掩码判断【与】运算是否相等,不等则说明不在同一掩码内,提前退出
if (mask[i] & ip1[i]) != (mask[i] & ip2[i]):
b = False
break
print("0") if b else print("2")
except:
break