题解 | #识别有效的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);
        int[] res = new int[7];

        while (in.hasNextLine()) {
            String s = in.nextLine();
            int ret = checkStr(s);
            if (ret == 7) {
                continue;
            }

            res[ret % 8]++;
            if (ret > 7) {
                res[6]++;
            }
        }
        System.out.println(res[0] + " " + res[1] + " " + res[2] + " " + res[3] + " " +
                           res[4] + " " + res[5] + " " + res[6]);
    }


    static int checkStr(String str) { //检查字符串,低3位 0~4:a~e 5:不合法 7:不计数,第4位表示私有
        String[] ss = str.split("~");
        if (ss.length != 2) return 5;

        String[] ipArr = ss[0].split("\\.");
        if (ipArr.length != 4) return 5;

        String[] maskArr = ss[1].split("\\.");
        if (maskArr.length != 4) return 5;

        long ip = 0;
        for (String numStr : ipArr) {
            ip <<= 8;
            int num = 0;
            for (char c : numStr.toCharArray()) {
                num *= 10;
                if (c < '0' || c > '9') {
                    return 5;
                }
                num += c - '0';
            }
            if (num > 255) {
                return 5;
            }
            ip += num;
        }
        if (ip < (1L << 24)) {
            return 7;
        }
        if (ip < (128L << 24) && ip >= (127L << 24)) {
            return 7;
        }

        boolean flag0 = false;
        long mask = 0;
        for (String numStr : maskArr) {
            mask <<= 8;
            int num = 0;
            if (flag0) {
                if (numStr.equals("0")) {
                    continue;
                } else {
                    return 5;
                }
            }
            for (char c : numStr.toCharArray()) {
                num *= 10;
                if (c < '0' || c > '9') {
                    return 5;
                }
                num += c - '0';
            }
            if (num > 255) {
                return 5;
            }
            if (num == 255) {
                mask += 255;
            } else if (num == 254 || num == 252 || num == 248 || num == 240 || num == 224 ||
                       num == 192 || num == 128 || num == 0) {
                flag0 = true;
                mask += num;
            } else {
                return 5;
            }
        }
        if (mask == 0 || mask >= (1L << 32) - 1) {
            return 5;
        }
        int ret = 0;
        if (ip < (10L << 24)) {
            ret = 0;//A
        } else if (ip < (11L << 24)) {
            ret = 8;//A私
        } else if (ip < (127L << 24)) {
            ret = 0;//A
        } else if (ip < (((172L << 8) + 16) << 16)) {
            return 1;//B
        } else if (ip < (((172L << 8) + 32) << 16)) {
            return 9;//B私
        } else if (ip < (192L << 24)) {
            ret = 1;//B
        } else if (ip < (((192L << 8) + 168) << 16)) {
            ret = 2;//C
        } else if (ip < (((192L << 8) + 169) << 16)) {
            ret = 10;//C私
        } else if (ip < (224L << 24)) {
            ret = 2;//C
        } else if (ip < (240L << 24)) {
            ret = 3;//D
        } else {
            ret = 4;//E
        }
        return ret;
    }
}


全部评论

相关推荐

10-31 20:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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