看大家写的的脑壳大,还是自己写吧
识别有效的IP地址和掩码并进行分类统计
https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
import java.util.Scanner; public class Main { public static void main(String[] args) { // 统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。 Scanner in = new Scanner(System.in); MyCount myCount = new MyCount(); while (in.hasNextLine()) { String string = in.nextLine(); if (string.isEmpty()) { break; } // 分ip和子网掩码 String[] strings = string.split("~"); String ip = strings[0]; String[] ipSplit = ip.split("\\."); // 127/0 不纳入计算的ip if (isInvalidIp(ipSplit)) { continue; } // 注意同一行数据中,ip或者子网掩码其中一个错就当做一次累计 if (isError(ipSplit) || isErrorMask(strings[1].split("\\."))) { myCount.setErrorIpOrMask(myCount.getErrorIpOrMask() + 1); continue; } // 处理ip,包括私有的 handleIp(ipSplit, myCount); } System.out.println(myCount.getA() + " " + myCount.getB() + " " + myCount.getC() + " " + myCount.getD() + " " + myCount.getE() + " " + myCount.getErrorIpOrMask() + " " + myCount.getpIp()); } private static boolean isErrorMask(String[] maskArr) { if (maskArr.length != 4 || !isNumericOrRange(maskArr)) { return true; } // 获得子网掩码的二进制字符串 char[] charArray = getBinaryString(maskArr); // 第一位非1 if (charArray[0] != '1') { return true; } boolean isZero = false; for (char c : charArray) { if (c == '0') { isZero = true; } else if (c == '1' && isZero) { return true; } } // 千万注意全是1的情况 if (!isZero) { return true; } return false; } private static char[] getBinaryString(String[] maskArr) { StringBuilder stringBuilder = new StringBuilder(); for (String string : maskArr) { int maskN = Integer.parseInt(string); /** * 子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码) * (注意二进制下全是1或者全是0均为非法子网掩码) */ // 8位2进制数即0~255范围 String binaryString = Integer.toBinaryString(maskN); for (int i = 0; i < 8 - binaryString.length(); i++) { stringBuilder.append("0"); } stringBuilder.append(binaryString); } char[] charArray = stringBuilder.toString().toCharArray(); return charArray; } private static void handleIp(String[] ipSplit, MyCount myCount) { String ipFragment = ipSplit[0]; int ipF = Integer.parseInt(ipFragment); // 公网ip abcde if (ipF >= 1 && ipF <= 126) { myCount.setA(myCount.getA() + 1); } if (ipF >= 128 && ipF <= 191) { myCount.setB(myCount.getB() + 1); } if (ipF >= 192 && ipF <= 223) { myCount.setC(myCount.getC() + 1); } if (ipF >= 224 && ipF <= 239) { myCount.setD(myCount.getD() + 1); } if (ipF >= 240 && ipF <= 255) { myCount.setE(myCount.getE() + 1); } int ipF2 = Integer.parseInt(ipSplit[1]); boolean pIp = false; // 私网ip if (ipF == 10) { pIp = true; } if (ipF == 172 && ipF2 >= 16 && ipF2 <= 31) { pIp = true; } if (ipF == 192 && ipF2 == 168) { pIp = true; } if (pIp) { myCount.setpIp(myCount.getpIp() + 1); } } private static boolean isError(String[] ipSplit) { boolean errorIp = false; // 非4段 if (ipSplit.length != 4) { errorIp = true; } else { for (String str : ipSplit) { // 不是数字 if (!isNumeric(str)) { errorIp = true; break; } int ipNum = Integer.parseInt(str); // 范围不对 if (ipNum < 0 || ipNum > 255) { errorIp = true; break; } } } return errorIp; } // 无效ip public static boolean isInvalidIp(String[] ip) { // 能被"."分成4段且开头为0或者127 if (ip.length == 4 && (ip[0].equals("0") || ip[0].equals("127"))) { return true; } return false; } public static boolean isNumeric(String str) { for (char c : str.toCharArray()) { if (!Character.isDigit(c)) { return false; } } return str.length() > 0; } public static boolean isNumericOrRange(String[] str) { for (int i = 0; i < str.length; i++) { if (!isNumeric(str[i])) { return false; } int range = Integer.parseInt(str[i]); // 遍历每一段看是否符合0~255 if (range < 0 || range > 255) { return false; } } return true; } static class MyCount { private long a = 0; private long b = 0; private long c = 0; private long d = 0; private long e = 0; // 错误ip和掩码 private long errorIpOrMask = 0; // 私有ip private long pIp = 0; public MyCount() { } public long getA() { return a; } public void setA(long a) { this.a = a; } public long getB() { return b; } public void setB(long b) { this.b = b; } public long getC() { return c; } public void setC(long c) { this.c = c; } public long getD() { return d; } public void setD(long d) { this.d = d; } public long getE() { return e; } public void setE(long e) { this.e = e; } public long getErrorIpOrMask() { return errorIpOrMask; } public void setErrorIpOrMask(long errorIpOrMask) { this.errorIpOrMask = errorIpOrMask; } public long getpIp() { return pIp; } public void setpIp(long pIp) { this.pIp = pIp; } } }