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

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

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



import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int wrongCount = 0;
        int aCount = 0;
        int bCount = 0;
        int cCount = 0;
        int dCount = 0;
        int eCount = 0;
        int privateCount = 0;
        while (in.hasNextLine()) {
            String str = in.nextLine();
            List ipList = new ArrayList<>();
            // 如果输入的行为空,就结束循环
            if (str.isEmpty()) {
                break;
            }
            boolean isEmptyFlag = false;
            for (String ipPart : str.split("~")[0].split("\\.")) {
                if (ipPart.isEmpty()) {
                    isEmptyFlag = true;
                }
                ipList.add(ipPart);
            }
            if (isEmptyFlag) {
                wrongCount++;
                continue;
            }
            // 忽略0或127
            if(ipList.get(0).equals("0") || ipList.get(0).equals("127")){
                continue;
            }

            // 错误IP地址或错误掩码识别
            if (wrongIp(str)) {
                wrongCount++;
                continue;
            }

            // ABCDE 识别
            int firstIp = Integer.valueOf(ipList.get(0).toString());
            int secondIp = Integer.valueOf(ipList.get(1).toString());
            if (firstIp >= 1 && firstIp <= 126) {
                // A
                aCount++;
            }

            if (firstIp >= 128 && firstIp <= 191) {
                // B
                bCount++;
            }

            if (firstIp >= 192 && firstIp <= 223) {
                // C
                cCount++;
            }

            if (firstIp >= 224 && firstIp <= 239) {
                // D
                dCount++;
            }

            if (firstIp >= 240 && firstIp <= 255) {
                // E
                eCount++;
            }

            // 私有的
            if (firstIp == 10
                    || (firstIp == 172 && (secondIp >= 16 && secondIp <= 31))
                    || (firstIp == 192 && secondIp == 168)) {
                privateCount++;
            }


        }

        System.out.print(aCount + " ");
        System.out.print(bCount + " ");
        System.out.print(cCount + " ");
        System.out.print(dCount + " ");
        System.out.print(eCount + " ");
        System.out.print(wrongCount + " ");
        System.out.print(privateCount);


    }

    public static boolean wrongIp(String str) {
        String regex = "1+0+";
        String binaryStr = "";
        // 错误IP地址或错误掩码识别
        for (String maskPart : str.split("~")[1].split("\\.")) {

            if (maskPart.isEmpty()) {
                return true;
            }
            String binaryIp = Integer.toBinaryString(Integer.valueOf(maskPart));
            // 如果不是八位则补全
            while (binaryIp.length() < 8) {
                binaryIp = "0".concat(binaryIp);
            }
            binaryStr += binaryIp;

        }
        if (!binaryStr.matches(regex)) {
            return true;
        }
        return false;

    }

}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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