题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
http://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
看了大家的代码我发现我想多了。。。
但我这个应该是无敌版的,以后不管他再加什么样的新的测试用例我百分百必能过!!
比如adsf.1234.5123.1234123.51251.234.123.5123.5.23415.1234.5.341.5g.13.g.1346.34.t34.1t.134g13.y.13t4.hu89
def y(list1):
list1 = list(map(int,list1))
str1 = ''
for i in list1:
str1 = str1 + '0'*(10-len(bin(i))) + bin(i)[2:]
str1 = str1.lstrip('1')
if "1" in str1:
return 0
else:
return 1
while 1:
try:
yanma =input().split(".")
ip1 = input().split(".")
ip2 = input().split(".")
n = 0
if len(yanma) != 4 or len(ip1) != 4 or len(ip2) != 4:
n=1
else:
for i in range(4):
if yanma[i].isdecimal() and ip1[i].isdecimal() and ip2[i].isdecimal():
if 0 <= int(yanma[i]) <=255 and 0 <= int(ip1[i]) <=255 and 0 <= int(ip2[i]) <=255:
ip1[i] = int(ip1[i]) & int(yanma[i])
ip2[i] = int(ip2[i]) & int(yanma[i])
else:
n=1
break
else:
n=1
break
if n == 1:
print(1)
else:
if y(yanma):
if ip1 == ip2:
print(0)
else:
print(2)
else:
print(1)
except:
break