题解 | #识别有效的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 a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int error = 0;
        int pr = 0;
        while (in.hasNext()) {
            String ain = in.nextLine();
            if (null == ain || ain.length() < 17 || ain.length() > 31) {
                continue;
            }
            String[] ainstr = ain.split("~");
            if (ainstr.length != 2) {
                continue;
            }
            String ip = ainstr[0];
            String mask = ainstr[1];
            int first = getNum(ip, 0);
            if (first == 0 || first == 127) {
                continue;
            }
            if (cutMask(mask)) { //子网掩码不合理
                error++;
                continue;
            }
            int ipFirst = getNum(ip, 0);
            if (ip.split("\\.").length != 4) {
                error++;
                continue;
            }
            if (ipFirst >= 1 && ipFirst <= 255) {
                int ipTwo = getNum(ip, 1);
                int ipThree = getNum(ip, 2);
                int ipFour = getNum(ip, 3);

                //处理A~E
                if (ipTwo >= 0 && ipTwo <= 255
                        && ipThree >= 0 && ipThree <= 255
                        && ipFour >= 0 && ipFour <= 255) {
                    if (ipFirst >= 1 && ipFirst <= 126) {
                        a++;
                    } else if (ipFirst >= 128 && ipFirst <= 191) {
                        b++;
                    } else if (ipFirst >= 192 && ipFirst <= 223) {
                        c++;
                    } else if (ipFirst >= 224 && ipFirst <= 239) {
                        d++;
                    } else if (ipFirst >= 240 && ipFirst <= 255) {
                        e++;
                    } else {
                        error++;
                    }
                    //处理私网ip
                    if (ipFirst == 10 || (ipFirst == 172
                                          && ipTwo >= 16 && ipTwo <= 31) || (ipFirst == 192
                                                  && ipTwo == 168)) {
                        pr++;
                    }
                } else {
                    error++;
                }

                continue;
            } else {
                error++;
                continue;
            }
        }
        System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + error + " "
                           + pr);
    }
    private static int getNum(String arr, int ind) {
        String[] astr = arr.split("\\.");
        return Integer.parseInt(astr[ind]);
    }
    private static boolean  cutMask(String mask) {
        String[] maArr = mask.split("\\.");
        if (maArr.length != 4) {
            return true;
        }

        String zz = toBinary(maArr[0]) +
                    toBinary(maArr[1]) + toBinary(maArr[2])+toBinary(maArr[3]);
        if (!zz.contains("1") || !zz.contains("0")) {
            return true;
        }
        if (!zz.matches("[1]{1,}[0]{1,}")) {
            return true;
        }
        return false;
    }
     public static String toBinary(String num) {
        String numBinary = Integer.toBinaryString(Integer.valueOf(num));
        //当不足8位需前面补齐0
        while (numBinary.length() < 8) {
            numBinary = "0" + numBinary;
        }
        return numBinary;
    }
}

知识要点提示:

1 正则表达式zz.matches("[1]{1,}[0]{1,}"),表示前面都为1,后面都为0,当拼接mask的各个位的二进制数时,如果不足8位需前面补齐0;

2 arr.split("\\."),当以特殊字符分割,需加\\进行转译

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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