题解 | #识别有效的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 in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数
        //A类地址从1.0.0.0到126.255.255.255;
        //B类地址从128.0.0.0到191.255.255.255;
        //C类地址从192.0.0.0到223.255.255.255;
        //D类地址从224.0.0.0到239.255.255.255;
        //E类地址从240.0.0.0到255.255.255.255
        // 类似于【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略
        // 子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)(注意二进制下全是1或者全是0均为非法子网掩码)
        int[] ints = {0, 0, 0, 0, 0, 0, 0};
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String input = in.nextLine();
            if (!input.contains(".")) {
                ints[5] += 1;
                continue;
            }
            String[] strs = input.split("~");
            String ip1 = strs[0];

            // 判断IP
            String[] array1 = ip1.split("\\.");
            if (Integer.parseInt(array1[0]) == 0 || Integer.parseInt(array1[0]) == 127) {
                continue;
            }
            if (array1.length != 4) {
                ints[5] += 1;
                continue;
            }
            if (!validIp(array1)) {
                ints[5] += 1;
                continue;
            }

            // 判断掩码
            String ip2 = strs[1];
            String[] array2 = ip2.split("\\.");
            StringBuilder result = new StringBuilder();
            for (String s : array2) {
                String tmp = Long.toBinaryString(Long.parseLong(s));
                while (tmp.length() % 8 != 0) {
                    tmp = "0" + tmp;
                }
                result.append(tmp);
            }
            char[] int01 = result.toString().toCharArray();
            int count1 = 0;
            int count0 = 0;
            for (int i = 0; i < 32; i++) {
                if (int01[i] == '1') {
                    count1++;
                } else {
                    break;
                }
            }
            for (int i = 31; i >= 0; i--) {
                if (int01[i] == '0') {
                    count0++;
                } else {
                    break;
                }
            }
            if (count1 + count0 < 32 || count1 == 32 || count0 == 32) {
                ints[5] += 1;
                continue;
            }

            // IP
            if (Integer.parseInt(array1[0]) >= 240 && Integer.parseInt(array1[0]) <= 255) {
                ints[4] += 1;
            } else if (Integer.parseInt(array1[0]) >= 224 &&
                       Integer.parseInt(array1[0]) <= 239) {
                ints[3] += 1;
            } else if (Integer.parseInt(array1[0]) >= 192 &&
                       Integer.parseInt(array1[0]) <= 223) {
                ints[2] += 1;
            } else if (Integer.parseInt(array1[0]) >= 128 &&
                       Integer.parseInt(array1[0]) <= 191) {
                ints[1] += 1;
            } else if (Integer.parseInt(array1[0]) >= 1 &&
                       Integer.parseInt(array1[0]) <= 126) {
                ints[0] += 1;
            }
            if (Integer.parseInt(array1[0]) == 10
                    || Integer.parseInt(array1[0]) == 172 && Integer.parseInt(array1[1]) >= 16 &&
                    Integer.parseInt(array1[1]) <= 31
                    || Integer.parseInt(array1[0]) == 192 && Integer.parseInt(array1[1]) == 168) {
                ints[6] += 1;
            }
        }
        System.out.println(ints[0] + " " +
                           ints[1] + " " +
                           ints[2] + " " +
                           ints[3] + " " +
                           ints[4] + " " +
                           ints[5] + " " +
                           ints[6]);
    }
    private static boolean validIp(String[] array1) {
        for (int index = 0; index < array1.length; index++) {
            if ("".equals(array1[index])) {
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

07-15 12:24
重庆大学 运营
坏消息:和好工作擦肩而过
给点吧求求了:怎么可能因为差几秒,估计就是简历更好看婉拒了
点赞 评论 收藏
分享
码农索隆:你告诉他,你看他也一般
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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