题解 | #识别有效的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);
        //分别代表A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数
        int[] count = new int[7];
        while(in.hasNextLine()){
            String str = in.nextLine();
            String[] addr = str.split("~");
            //先判断地址是否合法
            Info info = new Info(-1, 0);
            boolean ipflag = ipjudge(addr[0], info);
            boolean maskflag = maskjudge(addr[1]);
            //若合法,则判断属于哪一类
            if(info.type == 6){
                continue;
            }
            if(ipflag && maskflag){
                count[info.type]++;
                if(info.num != 0){
                    count[6]++;
                }
            }else{
                count[5]++;
            }
        }
        for(int i = 0; i < 7; i++){
            System.out.print(count[i]);
            if(i != 6){
                System.out.print(" ");
            }
        }
    }
    public static boolean ipjudge(String str, Info info){
        String[] ip = str.split("\\.");
        if(ip.length < 4){
            return false;
        }
        int[] nums = new int[4];
        for(int i = 0; i < ip.length; i++){
            int sum = 0;
            for(int j = 0; j < ip[i].length(); j++){
                char ch = ip[i].charAt(j);
                if(ch>= '0' && ch <= '9'){
                    sum = sum * 10 + (int)(ch - '0');
                }else{  //遇到其它字符,直接返回false
                    return false;
                }
            }
            if(sum > 255){
                return false;
            }else{
                nums[i] = sum;
            }
        }
        //判断ip地址的类型
        if(nums[0] >= 1 && nums[0] <= 126){
            info.type = 0;
        }else if(nums[0] >= 128 && nums[0] <= 191){
            info.type = 1;
        }else if(nums[0] >= 192 && nums[0] <= 223){
            info.type = 2;
        }else if(nums[0] >= 224 && nums[0] <= 239){
            info.type = 3;
        }else if(nums[0] >= 240 && nums[0] <= 255){
            info.type = 4;
        }
        //判断是否为私网ip
        if(        (nums[0] == 10)
                || (nums[0] == 172 && nums[1] >= 16 && nums[1] <= 31)
                || (nums[0] == 192 && nums[1] == 168)){
            info.num = 1;
        }
        if(nums[0] == 0 || nums[0] == 127){
            info.type = 6;
        }
        return true;
    }

    public static boolean maskjudge(String str){
        String[] mask = str.split("\\.");
        StringBuffer sb = new StringBuffer();
        //十进制转二进制
        for(int i = 0; i < mask.length; i++){
            int sum = 0;
            for(int j = 0; j < mask[i].length(); j++){
                char ch = mask[i].charAt(j);
                if(ch>= '0' && ch <= '9'){
                    sum = sum * 10 + (int)(ch - '0');
                }else{  //遇到其它字符,直接返回false
                    return false;
                }
            }
            if(sum < 0 || sum > 255){
                return false;
            }
            String number = Integer.toBinaryString(sum);
            for(int j = 0; j < 8 - number.length(); j++){
                sb.append('0');
            }
            sb.append(number);
        }
        String mask2 = sb.toString();
        int loc1 = -1;
        int loc0 = mask2.length();
        for(int i = 0; i < mask2.length(); i++){
            if(mask2.charAt(i) == '0'){
                loc0 = i;
            }else{
                loc1 = i;
            }
            if(loc0 < loc1){    //如果出现了0在1之前,则说明子网掩码不合法
                return false;
            }
        }
        if(loc1 == -1 || loc0 == mask2.length()){
            return false;
        }
        return true;
    }
}

class Info{
    int type;
    int num;

    public Info(Integer type, Integer num) {
        this.type = type;
        this.num = num;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务