题解 | #识别有效的IP地址和掩码并进行分类统计#

识别有效的IP地址和掩码并进行分类统计

https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682

A,B,C,D,E,NO,__ = 0,0,0,0,0,0,0
while True:
    try:
        n = input().split('~')
        zi = n[0]
        yan = n[1]
        res_list = zi.split('.')
        res_list1 = yan.split('.')
        if res_list[0] != '0' and res_list[0] != '127':
            if '' in res_list or '' in res_list1 or len(res_list) != 4 or len(res_list1) != 4:
                NO += 1
            else:
                res_list = list(map(int,res_list))
                res_list1 = list(map(int,res_list1))
                res_str = ''
                for i in res_list1:
                    res_str += str(bin(i))[2:].rjust(8,'0')
                res_str1 = sorted(res_str,reverse=True)
                if list(res_str) != res_str1 or len(set(res_str1)) != 2 or res_str1.count('1') == 1:
                    NO += 1
                else:
                    for i in range(4):
                        if res_list[i] not in range(0,256) or res_list1[i] not in range(0,256):
                            NO +=1
                            break
                    else:
                        if res_list[0] in range(1,127):
                            A += 1
                            if res_list[0] == 10:
                                __ += 1
                        elif res_list[0] in range(128,192):
                            B += 1
                            if res_list[0] == 172 and res_list[1] in range(16,32):
                                __ += 1
                        elif res_list[0] in range(192,224):
                            C += 1
                            if res_list[0] == 192 and res_list[1] == 168:
                                __ += 1
                        elif res_list[0] in range(224,240):
                            D += 1
                        elif res_list[0] in range(240,256):
                            E += 1
    except:
        break
print(A,end=' ')
print(B,end=' ')
print(C,end=' ')
print(D,end=' ')
print(E,end=' ')
print(NO,end=' ')
print(__)

全部评论

相关推荐

徐新高:号已经废了 建议重开一个账号投简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务