题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
http://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
#判断IP地址是否有效
def isOK(s):
for i in range(len(s)):
if s[i] > 255 or s[i]<0:
return False
return True
# 判断掩码是否正确
def isPr(s):
numob = []
for i in s:
i = bin(int(i))
i = i[2:]
numob.append(i.zfill(8))
whole_mask = "".join(numob)
if whole_mask.rfind("1")+1 == whole_mask.find("0"):
return True
return False
while True:
try:
s = list(map(int, input().split('.')))
m1 = list(map(int, input().split('.')))
m2 = list(map(int, input().split('.')))
if isOK(s) == False or isOK(m1) == False or isOK(m2) == False or isPr(s)==False:
print(1)
elif s[0]&m1[0]==s[0]&m2[0] and s[1]&m1[1]==s[1]&m2[1] and s[2]&m1[2]==s[2]&m2[2] and s[3]&m1[3]==s[3]&m2[3]:
print(0)
else:
print(2)
except:
break