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

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

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] result = { 0, 0, 0, 0, 0, 0, 0 };

        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String ipAndMask = in.nextLine();

            if (ifIgnore(ipAndMask)) {
                continue;
            }
            if (ifIllegal(ipAndMask)) {
                result[5]++;
                continue;
            }
            if (ifPrivate(ipAndMask)) {
                result[6]++;
            }
            char tempChar = ifABCDE(ipAndMask);
            switch (tempChar) {
                case 'A':
                    result[0]++;
                    break;
                case 'B':
                    result[1]++;
                    break;
                case 'C':
                    result[2]++;
                    break;
                case 'D':
                    result[3]++;
                    break;
                case 'E':
                    result[4]++;
                    break;
                default:
                    break;
            }
        }
        // 输出语句
        for (int i = 0; i < 6; i++) {
            System.out.print(result[i] + " ");
        }
        System.out.print(result[6] + "\n");
        in.close();
    }

    /*
     * 返回n个长度的字符串数组存储ip位和掩码位(一般为8)
     * 129.125.4.~255.255.255.255 这种时候长度为7
     * 129.125.4.~255.255.255. 这种时候长度为6
     */
    public static String[] toStringArr(String ipAndMask) {
        String[] tempIp = ipAndMask.split("~")[0].split("\\."); // ip位
        String[] tempMask = ipAndMask.split("~")[1].split("\\."); // 掩码位
        int n = tempIp.length + tempMask.length;
        String[] result = new String[n];

        // index0~3存ip,4~7存mask
        for (int i = 0; i < tempIp.length; i++) {
            result[i] = tempIp[i];
        }
        for (int i = tempIp.length; i < n; i++) {
            result[i] = tempMask[i - tempIp.length];
        }
        return result;
    }

    /*
     * 将掩码转换为32位的二进制字符串
     * 例如255.255.128.0 --> 11111111 11111111 10000000 00000000
     */
    public static String toBinary(String ipAndMask) {
        String[] input = toStringArr(ipAndMask);// 4~7存储掩码
        String result = new String();

        for (int i = 4; i < input.length; i++) {
            Integer maskNum = Integer.valueOf(input[i]);
            String tempStr = Integer.toBinaryString(maskNum);
            // 补齐8位
            while (tempStr.length() < 8) {
                tempStr = "0" + tempStr;
            }
            result += tempStr;
        }
        return result;
    }

    // 需要忽略返回true,否则返回false
    public static boolean ifIgnore(String ipAndMask) {
        String[] input = toStringArr(ipAndMask);
        switch (input[0]) {
            case "0":
            case "127":
                return true;
            default:
                return false;
        }
    }

    // ip错误返回true,否则返回false。默认ip不被忽略
    public static boolean ifIllegal(String ipAndMask) {
        String[] input = toStringArr(ipAndMask);
        String mask = toBinary(ipAndMask);

        // 先遍历,如果input中存在""或者<0或者>255或者input.length != 8,说明ip错误,return true;
        for (String str : input) {
            if (input.length != 8 || str.equals("") || Integer.valueOf(str) < 0 ||
                    Integer.valueOf(str) > 255) {
                //System.out.println("a");
                return true;
            }
        }

        /*
         * 1111111111...00000 中包含11、10、00 --> 合法
         * 1111111111...11111 中包含11 --> 非法
         * 0000000000...00000 中包含00 --> 非法
         * 1111110010...11010 中包含11、10、00、01 --> 非法
         * 所以当mask包含10并且不包含01时说明掩码合法
         */
        if (mask.contains("10") && !mask.contains("01")) {
            //System.out.println("b");
            return false;
        }
        return true;
    }

    // 返回'A'|'B'|'C'|'D'|'E'表示ip所处类。默认ip合法
    public static char ifABCDE(String ipAndMask) {
        String[] input = toStringArr(ipAndMask);

        Integer tempi = Integer.valueOf(input[0]);
        if (tempi > 0 && tempi < 127) {
            return 'A';
        } else if (tempi > 127 && tempi < 192) {
            return 'B';
        } else if (tempi > 191 && tempi < 224) {
            return 'C';
        } else if (tempi > 223 && tempi < 240) {
            return 'D';
        } else {
            return 'E';
        }
    }

    // 私网ip返回true,否则返回false。默认ip合法
    public static boolean ifPrivate(String ipAndMask) {
        String[] input = toStringArr(ipAndMask);

        Integer tempi = Integer.valueOf(input[0]);
        Integer tempi2 = Integer.valueOf(input[1]);

        // 10.0.0.0 ~ 10.255.255.255
        if (tempi == 10) {
            //System.out.println("c");
            return true;
        }

        // 172.16.0.0 ~ 172.31.255.255
        if (tempi == 172 && tempi2 > 15 && tempi2 < 32) {
            //System.out.println("c");
            return true;
        }
        // 192.168.0.0 ~ 192.168.255.255
        if (tempi == 192 && tempi2 == 168) {
            //System.out.println("c");
            return true;
        }

        return false;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
4887次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1093次浏览 27人参与
# MiniMax求职进展汇总 #
22850次浏览 293人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6891次浏览 36人参与
# 简历第一个项目做什么 #
31243次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186328次浏览 1114人参与
# 米连集团26产品管培生项目 #
4056次浏览 196人参与
# 面试紧张时你会有什么表现? #
30317次浏览 188人参与
# 简历中的项目经历要怎么写? #
309349次浏览 4149人参与
# 网易游戏笔试 #
6302次浏览 83人参与
# 职能管理面试记录 #
10676次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6843次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56694次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160388次浏览 1105人参与
# 小红书求职进展汇总 #
226842次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62375次浏览 727人参与
# 你怎么看待AI面试 #
179242次浏览 1162人参与
# 正在春招的你,也参与了去年秋招吗? #
362478次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92122次浏览 896人参与
# 机械求职避坑tips #
94395次浏览 567人参与
# 校招笔试 #
466042次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27078次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务