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

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

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int countA = 0,countB = 0,countC = 0,countD = 0,countE = 0,countWrong = 0,countPrivate = 0;
        while (sc.hasNextLine()){
            String str = sc.nextLine();
            //双回车退出hasNextLine;
            if("".equals(str)){
                break;
            }
            //分离出ip地址和子网掩码
            String[] ipAndMask = str.split("\\~");
            //分离出第一位,判断是否忽略,属于哪一类
            int num1 = getIpSeg(ipAndMask[0],0);
            if(num1 == 0 || num1 == 127){
                continue;
            }
            //判断子网掩码是否非法
            if(maskIsIllegal(ipAndMask[1])){
                countWrong++;
                continue;
            }
            //判断ip地址是否非法
            if(ipIsIllegal(ipAndMask[0])){
                countWrong++;
                continue;
            }
            //判断合法的
            if(num1 >= 1 && num1 <= 126){
                countA++;
            }else if(num1 >= 128 && num1 <= 191){
                countB++;
            }else if(num1 >= 192 && num1 <= 223){
                countC++;
            }else if(num1 >= 224 && num1 <= 239){
                countD++;
            }else if(num1 >= 240 && num1 <= 255){
                countE++;
            }
            //判断私有
            int num2 = getIpSeg(ipAndMask[0], 1);
            if(num1 == 10 || (num1 == 172 && num2 >= 16 && num2 <= 31) || (num1 == 192 && num2 == 168)){
                countPrivate++;
            }
        }
        System.out.println(countA+" "+countB+" "+countC+" "+countD+" "+countE+" "+countWrong+" "+countPrivate);
    }

    private static boolean ipIsIllegal(String s) {
        //分离每位
        String[] num = s.split("\\.");
        if(num.length != 4){
            return true;
        }
        if(Integer.parseInt(num[0]) > 255 || Integer.parseInt(num[1]) > 255 || Integer.parseInt(num[2]) > 255 || Integer.parseInt(num[3]) > 255){
            return true;
        }
        return false;
    }

    private static boolean maskIsIllegal(String s) {
        //分离每位
        String[] num = s.split("\\.");
        if(num.length != 4){
            return true;
        }
        //把子网掩码转为二进制字符串
        String mask = toBinary(num);
        if(!mask.matches("[1]{1,}[0]{1,}")){
            return true;
        }
        return false;
    }

    private static String toBinary(String[] num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num.length; i++) {
            StringBuilder s = new StringBuilder(Integer.toBinaryString(Integer.parseInt(num[i])));
            while(s.length() < 8){
                s.insert(0, "0");
            }
            sb.append(s);
        }
        return sb.toString();
    }


    private static int getIpSeg(String s,int index) {
        //分离每位
        String[] num = s.split("\\.");
        return Integer.parseInt(num[index]);
    }
}
全部评论

相关推荐

珩珺:那些经历都太大太空了,实习的情况不了解,大创项目连名字、背景、目的及意义都没体现出来;地摊经济更是看完连卖的什么产品都不知道,项目成果直接写营收多少都更直观真实一点;后面那个校文体部的更是工作内容是组织活动整理流程,成果变成了当志愿者,而且你们学校本科学生会大一入学就直接当部长吗,志愿里面还提到了疫情防控,全面解封是22年12月的事情,可能时间上也有冲突。可能你花了钱人家就用AI给你随便写了点内容改了一下,没什么体现个性化的点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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