题解 | #识别有效的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); // 注意 hasNext 和 hasNextLine 的区别 int[] result = new int[] {0, 0, 0, 0, 0, 0, 0}; while (in.hasNextLine()) { // 注意 while 处理多个 case String input = in.nextLine(); String[] sp = input.split("~"); String sp1 = sp[0]; // ip地址 String sp2 = sp[1]; // 子网掩码 if (null == tellIpType(sp1)) { // 直接忽略 continue; } else { String tellIpType = tellIpType(sp1); if ( !isMask(sp2) ) { // 错误ip result[5] = result[5] + 1 ; continue; } if ("illegalIp".equals(tellIpType)) { // 错误ip result[5] = result[5] + 1 ; } if ("A".equals(tellIpType)) { // 错误的 result[0] = result[0] + 1 ; } if ("B".equals(tellIpType)) { // 错误的 result[1] = result[1] + 1 ; } if ("C".equals(tellIpType)) { // 错误的 result[2] = result[2] + 1 ; } if ("D".equals(tellIpType)) { // 错误的 result[3] = result[3] + 1 ; } if ("E".equals(tellIpType)) { // 错误的 result[4] = result[4] + 1 ; } if (isPrivate(sp1)) { // 错误的 result[6] = result[6] + 1 ; } } } for(int i :result){ System.out.print(i+" "); } } // 判断是否属于私有云 public static boolean isPrivate(String ip) { String[] split = ip.split("\\."); int i0 = Integer.parseInt(split[0]); int i1 = Integer.parseInt(split[1]); int i2 = Integer.parseInt(split[2]); int i3 = Integer.parseInt(split[3]); if (i0 == 10 && (i1 >= 0 && i1 <= 255) && (i2 >= 0 && i2 <= 255) && (i3 >= 0 && i3 <= 255)) { return true; } if (i0 == 172 && (i1 >= 16 && i1 <= 31) && (i2 >= 0 && i2 <= 255) && (i3 >= 0 && i3 <= 255)) { return true; } if (i0 == 192 && i1 == 168 && (i2 >= 0 && i2 <= 255) && (i3 >= 0 && i3 <= 255)) { return true; } return false; } // 判断属于那一类的 public static String tellIpType(String ip) { String[] split = ip.split("\\."); //如果后三个数字有大于255的,则属于非法ip for (int c = 1; c < split.length; c++) { if (split[c].trim().equals("") || Integer.parseInt(split[c]) > 255) { return "illegalIp"; } } //如果是0开头与127开头的,则返回一个null if (split[0].equals("0") || split[0].equals("127")) { return null; } int type = Integer.parseInt(split[0]); //区分A,B,C,D,E类IP if (type >= 1 && type <= 126) { return "A"; } else if (type >= 128 && type <= 191) { return "B"; } else if (type >= 192 && type <= 223) { return "C"; } else if (type >= 224 && type <= 239) { return "D"; } else if (type >= 240 && type <= 255) { return "E"; } else { return "illegalIp"; } } public static boolean isMask(String mask) { String[] split = mask.split("\\."); StringBuilder sb = new StringBuilder(); for (String s : split) { if (s.trim().equals("")) { return false; } int i = Integer.parseInt(s); //如果有数字大于255,则直接返回false if (i > 255) { return false; } String binary = Integer.toBinaryString(i); //如果长度小于8,则在前面补0 while (binary.length() < 8) { binary = "0".concat(binary); } sb.append(binary); } //32位二进制数中需要同时存在0和1,且不存在01 return sb.toString().contains("1") && sb.toString().contains("0") && !sb.toString().contains("01"); } }