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

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

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

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);// 做题不看子网掩码对应
        int aNum = 0;// 1-126 255. !10
        int bNum = 0;// 128-191 255.255. !172.16-31
        int cNum = 0;// 192-223 255.255.255 !192.168
        int dNum = 0;// 224-239 无子网掩码
        int eNum = 0;// 240-255 无子网掩码
        int errNum =
            0;// 错误IP地址或错误掩码 isValid 255.255.255.32就是一个非法的掩码)(注意二进制下全是1或者全是0均为非法子网掩码)f
        // 前面是连续的1,然后全是0
        int pNum = 0;// 私有IP 10----172.16-31---192.168 a,b,c下
        // 【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略 none 或者减去
        int noneNum = 0;
        while (in.hasNextLine()) {
            String str = in.nextLine();
            String[] strs = str.split("~");
            String str0 = strs[0];
            String[] strs0 = str0.split("\\.");
            String str1 = strs[1];
            String[] strs1 = str1.split("\\.");
if (!isNotValidIp(strs0)) {// || !isNotValidMask(strs1)) {
				errNum++;
			} else if (!isNotValidMask(strs1) && !("0".equals(strs0[0]) || "127".equals(strs0[0]))) {
				errNum++;
			} else {
				String temp = caseString(strs0);
				if ("a".equals(temp)) {
					aNum++;
				} else if ("ap".equals(temp)) {
					aNum++;
					pNum++;
				} else if ("b".equals(temp)) {
					bNum++;
				} else if ("bp".equals(temp)) {
					bNum++;
					pNum++;
				} else if ("c".equals(temp)) {
					cNum++;
				} else if ("cp".equals(temp)) {
					cNum++;
					pNum++;
				} else if ("d".equals(temp)) {
					dNum++;
				} else if ("e".equals(temp)) {
					eNum++;
				}
			}
		}
		errNum -= noneNum;
		System.out.println(aNum + " " + bNum + " " + cNum + " " + dNum + " " + eNum + " " + errNum + " " + pNum);
	}
    public static boolean isNotValidIp(String[] strs) {// err

        boolean b = false;
        if (strs.length != 4) {
            b = false;
        } else {
            for (int i = 0; i < strs.length; i++) {
                if (Integer.parseInt(strs[i]) < 0 || Integer.parseInt(strs[i]) > 255) {
                    b = false;
                    break;
                } else {
                    return b = true;
                }
            }
        }
        return b;

    }

    public static String caseString(String[] strs) {
        int a = Integer.parseInt(strs[0]);
        int b = Integer.parseInt(strs[1]);
        if (a == 0 || a == 127) {
            return "none";
        } else if (a > 0 && a < 127) {// 10
            if (a == 10)
                return "ap";

            return "a";
        } else if (a > 127 && a <= 191) {// !172.16-31
            if (a == 172 && (b >= 16 && b <= 31))
                return "bp";

            return "b";
        } else if (a > 191 && a <= 223) {// !192.168
            if (a == 192 && b == 168)
                return "cp";
            return "c";
        } else if (a > 223 && a <= 239) {
            return "d";
        } else if (a > 239 && a <= 255) {
            return "e";
        } else {
            return " ";
        }

    }

    public static boolean isNotValidMask(String[]
                                         strs) {// 掩码 String str, String[] strs)

        boolean b = false;
        StringBuilder sb = new StringBuilder();
        if (!isNotValidIp(strs)) {
            b = false;
        } else {
            for (int i = 0; i < strs.length; i++) {
                strs[i] = Integer.toBinaryString(Integer.parseInt(strs[i]));
                for (int j = 0; j < 8 - strs[i].length(); j++) {
                    sb.append("0");
                }
                sb.append(strs[i]);
            }
            String str = sb.toString();
            b = str.lastIndexOf("1") <
                str.indexOf("0");// 前面是连续的1,然后全是0
        }
        return b;

//      char ch = str.charAt(0);
//      int b0 = Integer.parseInt(strs[0]), b1 = Integer.parseInt(strs[1]), b2 = Integer.parseInt(strs[2]),
//              b3 = Integer.parseInt(strs[3]);
//      if (ch == 'a') {
//          if (b0 == 255 && b1 == 0 && b2 == 0 && b3 == 0) {
//              b = true;
//          }
//      } else if (ch == 'b') {
//          if (b0 == 255 && b1 == 255 && b2 == 0 && b3 == 0) {
//              b = true;
//          }
//      } else if (ch == 'c') {
//          if (b0 == 255 && b1 == 255 && b2 == 255 && b3 == 0) {
//              b = true;
//          }
//      } else {
//          b = false;
//      }
//      return b;

    }

}

全部评论

相关推荐

人醒着就会困:直接回答,"你刚才提到你是百度JAVA,那么你相对于字节JAVA的优势在哪里?"
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务