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

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

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int aCount = 0;//  A类IP数量
        int bCount = 0;//  B类IP数量
        int cCount = 0;//  C类IP数量
        int dCount = 0;//  D类IP数量
        int eCount = 0;//  E类IP数量
        int errCount = 0;// 错误数量
        int privateCount = 0;//  私有IP数量
        String type = "";
        while (scanner.hasNext()) {
            String str = scanner.nextLine().trim();
            if ("exit".equalsIgnoreCase(str)) {
                break;
            }
            String arr[] = str.split("~");
            if (arr.length < 2) {
                errCount++;
                continue;
            }
            String ip = arr[0].trim();//  IP地址
            String mask = arr[1].trim();//  掩码
            if (!isValidIp(ip)) {
                errCount++;
                continue;
            }
            if (isSpecialIp(ip)) {//特殊IP跳过
                continue;
            }
            if (!isValidMask(mask)) {
                errCount++;
                continue;
            }
            type = classifyIp(ip);
            switch (type) {
                case "A类":
                    aCount++;
                    break;
                case "B类":
                    bCount++;
                    break;
                case "C类":
                    cCount++;
                    break;
                case "D类":
                    dCount++;
                    break;
                case "E类":
                    eCount++;
                    break;
                default:
                    errCount++;
                    break;
            }
            if (isPrivateIp(ip)) {
                privateCount++;
            }

        }
        System.out.println(aCount + " " + bCount + " " + cCount + " " + dCount + " " +
                           eCount + " " + errCount + " " + privateCount);
        scanner.close();
    }
    //首先判断IP是否合法
    private static boolean isValidIp(String ip) {
        String[] ipArr = ip.split("\\.");
        //  判断IP地址的个数
        if (ipArr.length != 4) {
            // System.out.println("IP地址不是是4位数,不合法");
            return false;
        }
        for (String str : ipArr) {
            try {
                int num = Integer.parseInt(str);
                if (num < 0 || num > 255) {
                    //  System.out.println("IP地址不是数字,不合法");
                    return false;
                }
            } catch (NumberFormatException e) {
                return false;
            }

        }
        return true;
    }

    //其次判断特殊IP,特殊IP直接跳过
    //类似于 "0.*.*.*"和"127.*.*.*" IP 地址不属于上述输入的任意一类,也不属于不合法
    private static boolean isSpecialIp(String ip) {
        String[] ipArr = ip.split("\\.");
        String first = ipArr[0];
        if ("0".equals(first) || "127".equals(first)) {
            return true;
        }
        return false;
    }

    //最后判断掩码是否合法
    private static boolean isValidMask(String mask) {
        String[] maskArr = mask.split("\\.");
        StringBuilder sb = new StringBuilder();
        if (maskArr.length != 4) {
            // System.out.println("掩码位数不是4,不合法");
            return false;
        }
        for (String str : maskArr) {
            try {
                int num = Integer.parseInt(str);
                if (num < 0 || num > 255) {
                    ////  System.out.println("掩码不是数字,不合法");
                    return false;
                }
                String binStr = Integer.toBinaryString(num);//  转换成二进制字符串
                // System.out.println("转换的二进制为:" + binStr);
                binStr = String.format("%8s", binStr).replace(" ", "0");//  填充0
                // System.out.println("填充0后的二进制为:" + binStr);
                sb.append(binStr);
            } catch (NumberFormatException e) {
                // System.out.println("掩码不合法");
                return false;
            }

        }
        String binMask = sb.toString();
        //System.out.println("掩码转换后的二进制为:" + binMask);
        //检查是否是全1或全0
        String allZeros = "00000000000000000000000000000000";
        String allOnes = "11111111111111111111111111111111";
        if (binMask.equals(allZeros) || binMask.equals(allOnes)) {
            //System.out.println("掩码全是0或1,不合法");
            return false;
        }
        if (binMask.contains("01")) {
            //System.out.println("掩码含有“01”子串,不合法");
            return false;
        }
        return true;
    }

    //判断IP地址类别
    private static String classifyIp(String ip) {
        String[] ipArr = ip.split("\\.");
        int first = Integer.parseInt(ipArr[0]);
        String type = "";
        if (first >= 1 && first <= 126) {
            type = "A类";
        } else if (first >= 128 && first <= 191) {
            type = "B类";
        } else if (first >= 192 && first <= 223) {
            type = "C类";
        } else if (first >= 224 && first <= 239) {
            type = "D类";
        } else if (first >= 240 && first <= 255) {
            type = "E类";
        }
        return type;
    }

    //判断是否为私有IP
    private static boolean isPrivateIp(String ip) {
        String[] ipArr = ip.split("\\.");
        int first = Integer.parseInt(ipArr[0]);
        int second = Integer.parseInt(ipArr[1]);
        return first == 10 ||
               (first == 172 && second >= 16 && second <= 31) ||
               (first == 192 && second == 168);

    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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