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

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

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static int a = 0;
    private static int b = 0;
    private static int c = 0;
    private static int d = 0;
    private static int e = 0;
    private static int error = 0;
    private static int ip_private = 0;

    public static void main(String[] args) {
        List<String> ipList = new ArrayList<>();
        List<String> numList = new ArrayList<>();

        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String line = in.nextLine();
            String[] split = line.split("~");
            //
            String ip = split[0];
            String number = split[1];
            //判断ip是否合法
            if (Main.isRightIp(ip)) {
                //判断子网掩码是否合法
                if (Main.isRightNumber(number)) {
                    //判断类别
                    String[] ipSplit = ip.split("\\.");
                    String firstIp = ipSplit[0];
                    int firstValue = Integer.parseInt(firstIp);
                    if (firstValue >= 1 && firstValue <= 126) {
                        a++;
                        if (firstValue == 10) {
                            ip_private++;
                        }
                    } else if (firstValue >= 128 && firstValue <= 191) {
                        b++;
                        if (firstValue == 172) {
                            int secondValue = Integer.parseInt(ipSplit[1]);
                            if (secondValue > 18 && secondValue <= 31) {
                                ip_private++;
                            }
                        }
                    } else if (firstValue >= 192 && firstValue <= 223) {
                        c++;
                        if (firstValue == 192) {
                            int secondValue = Integer.parseInt(ipSplit[1]);
                            if (secondValue == 168) {
                                ip_private++;
                            }
                        }
                    } else if (firstValue >= 224 && firstValue <= 239) {
                        d++;
                    } else if (firstValue >= 240 && firstValue <= 255) {
                        e++;
                    }
                }
            }

        }
        System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + error + " "
                           + ip_private);

    }
    //将十进制转化为二进制
    public static String change(String result, int value) {
        if (value > 0) {
            result = value % 2 + result;
            value = value / 2;
            result = Main.change(result, value);
        }
        return result ;
    }

    //判断子网掩码是否合法
    public static boolean isRightNumber(String number) {

        try {
            String[] split = number.split("\\.");
            String lastResult = "";
            for (String string : split) {
                int value = Integer.parseInt(string);
                if (value < 0 || value > 255) {
                    error++;
                    return false;
                }
                //转化为二进制
                String result = "";
                result = Main.change(result, value);
                //补位8位
                if (result.length() != 8) {
                    int temp = 8 - result.length();
                    while (temp-- > 0) {
                        result = "0" + result;
                    }
                }
                lastResult = lastResult + result;
            }
            //判断是否连续的1和连续的0
            int index1 = lastResult.lastIndexOf('1');
            int index2 = lastResult.indexOf('0');
            if (index1 == lastResult.length() - 1) {
                error++;
                return false;
            }
            if (index2 == 0) {
                error++;
                return false;
            }
            if (index1 >= index2) {
                error++;
                return false;
            }
        } catch (Exception e) {
            error++;
            return false;
        }
        return true;
    }

    //判断ip地址是否正确
    public static boolean isRightIp(String ip) {

        try {
            String[] split = ip.split("\\.");
            //忽略
            if (split[0].equals("0") || split[0].equals("127")) {
                return false;
            }
            //不合法
            for (String string : split) {
                int value = Integer.parseInt(string);
                if (value < 0 || value > 255) {
                    error++;
                    return false;
                }
            }
        } catch (Exception e) {
            error++;
            return false;
        }
        return true;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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