题解 | #判断两个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
全部评论

相关推荐

刷牛客的单身狗很认真:全国可飞,支持007 上班时间,是吧?
点赞 评论 收藏
分享
11-13 10:17
门头沟学院 Java
昨天面美团,jvm,juc问的好深啊,感觉小林coding不太够喔,牛油们有没有什么推荐的八股网站嘛🕒&nbsp;岗位/面试时间👥&nbsp;面试题目🤔&nbsp;面试感受
明天不下雨了:小林Coding:https://xiaolincoding.com/ 全栈哥:https://www.pdai.tech/ Guide哥:https://javaguide.cn/ 秀哥:https://interviewguide.cn/ 沉默王二:https://javabetter.cn/home.html 磊哥:https://www.javacn.site/interview/basic/ 小傅哥:https://bugstack.cn/ 源码哥:https://doocs.github.io/source-code-hunter/#/ 各大厂的公众号技术文章和一些经典的书籍
面试太紧张了怎么办?
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务