题解 | #识别有效的IP地址和掩码并进行分类统计#
识别有效的IP地址和掩码并进行分类统计
https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
# 抄了一手判断子网掩码判断
a,b,c,d,e,f,g = 0,0,0,0,0,0,0
def error_mask(x):
noOb = []
for i in x:
i = bin(int(i))
i = i[2:]
noOb.append(i.zfill(8))
whole_mask = ''.join(noOb)
whole = int(whole_mask,base=10)
whole_str = str(whole)
whole0_find = whole_str.find("0")
whole1_rfind = whole_str.rfind("1")
if whole1_rfind+1==whole0_find:
return False
else:
return True
while True:
try:
D = input().split('~')
ip = D[0].split('.')
ym = D[1].split('.')
for m in ip:
if ip[0] == '0' or ip[0] == '127':
break
if error_mask(ym):
f += 1
break
if not m.isdigit():
f+=1
break
if int(m) not in range(0,256):
f+=1
break
else:
ip_1 = int(ip[0])
ip_2 = int(ip[1])
if ip_1 in range(1,127):
a+=1
elif ip_1 in range(128,192):
b+=1
elif ip_1 in range(192,224):
c+=1
elif ip_1 in range(224,240):
d+=1
elif ip_1 in range(240,256):
e+=1
if ip_1 == 10:
g+=1
elif ip_2 == 172 and ip_2 in range(16,32):
g+=1
elif ip_1 == 192 and ip_2 ==168:
g+=1
except:
break
print(a,b,c,d,e,f,g)
def error_mask(x):
noOb = []
for i in x:
i = bin(int(i))
i = i[2:]
noOb.append(i.zfill(8))
whole_mask = ''.join(noOb)
whole = int(whole_mask,base=10)
whole_str = str(whole)
whole0_find = whole_str.find("0")
whole1_rfind = whole_str.rfind("1")
if whole1_rfind+1==whole0_find:
return False
else:
return True
while True:
try:
D = input().split('~')
ip = D[0].split('.')
ym = D[1].split('.')
for m in ip:
if ip[0] == '0' or ip[0] == '127':
break
if error_mask(ym):
f += 1
break
if not m.isdigit():
f+=1
break
if int(m) not in range(0,256):
f+=1
break
else:
ip_1 = int(ip[0])
ip_2 = int(ip[1])
if ip_1 in range(1,127):
a+=1
elif ip_1 in range(128,192):
b+=1
elif ip_1 in range(192,224):
c+=1
elif ip_1 in range(224,240):
d+=1
elif ip_1 in range(240,256):
e+=1
if ip_1 == 10:
g+=1
elif ip_2 == 172 and ip_2 in range(16,32):
g+=1
elif ip_1 == 192 and ip_2 ==168:
g+=1
except:
break
print(a,b,c,d,e,f,g)
正浩创新EcoFlow公司福利 754人发布