题解 | #识别有效的IP地址和掩码并进行分类统计#
识别有效的IP地址和掩码并进行分类统计
http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
HashMap<String, Integer> countMap = new HashMap<>();
countMap.put("A", 0);
countMap.put("B", 0);
countMap.put("C", 0);
countMap.put("D", 0);
countMap.put("E", 0);
countMap.put("errorIPOrMask", 0);
countMap.put("privateIp", 0);
String str;
while ((str = br.readLine()) != null) {
if ("end".equals(str)){
break;
}
String[] ipAndMask = str.split("\\~");
if (ipAndMask.length != 2){
countMap.put("errorIPOrMask", countMap.get("errorIPOrMask") + 1);
continue;
}
String[] leftIpNums = ipAndMask[0].split("\\.");
String[] rightMasks = ipAndMask[1].split("\\.");
//判断位数是否正确
if (leftIpNums.length != 4 || rightMasks.length != 4){
countMap.put("errorIPOrMask",countMap.get("errorIPOrMask")+1);
continue;
}
//检查ip地址和掩码是否正确
if(!checkIp(leftIpNums) || !checkIp(rightMasks)){
countMap.put("errorIPOrMask",countMap.get("errorIPOrMask")+1);
continue;
}
if (("0").equals(leftIpNums[0]) || ("127").equals(leftIpNums[0])){//以0或127开头,不属于任何地址
continue;
}
//掩码是否正确
if (!checkMask(rightMasks)){
countMap.put("errorIPOrMask",countMap.get("errorIPOrMask")+1);
continue;
}
//判断ip类型
if (Integer.parseInt(leftIpNums[0]) >= 1 && Integer.parseInt(leftIpNums[0]) <= 126){ //A类
classify(leftIpNums,countMap,"A");
}else if (Integer.parseInt(leftIpNums[0]) >= 128 && Integer.parseInt(leftIpNums[0]) <= 191) { //B类
classify(leftIpNums,countMap,"B");
} else if (Integer.parseInt(leftIpNums[0]) >= 192 && Integer.parseInt(leftIpNums[0]) <= 223) { //C类
classify(leftIpNums,countMap,"C");
} else if (Integer.parseInt(leftIpNums[0]) >= 224 && Integer.parseInt(leftIpNums[0]) <= 239) { //D类
classify(leftIpNums,countMap,"D");
} else if (Integer.parseInt(leftIpNums[0]) >= 240 && Integer.parseInt(leftIpNums[0]) <= 255) { //E类
classify(leftIpNums,countMap,"E");
}
}
System.out.println(countMap.get("A")+" "+countMap.get("B")+" "+countMap.get("C")+" "+countMap.get("D")+" "+countMap.get("E")+" "+countMap.get("errorIPOrMask")+" "+countMap.get("privateIp"));
}
private static void classify(String[] ipAddress, HashMap<String,Integer> countMap, String type){
//Ip处理
if ("A".equals(type)){ //A类地址从1.0.0.0到126.255.255.255; 从10.0.0.0到10.255.255.255 A类 掩码:255.0.0.0
if ("10".equals(ipAddress[0])){
countMap.put("privateIp",countMap.get("privateIp")+1);
countMap.put("A",countMap.get("A")+1);
return;
}
countMap.put("A",countMap.get("A")+1);
}else if ("B".equals(type)){ //B类地址从128.0.0.0到191.255.255.255; 从172.16.0.0到172.31.255.255 B类 掩码:255.255.0.0
if ("172".equals(ipAddress[0]) && Integer.parseInt(ipAddress[1]) >= 16 && Integer.parseInt(ipAddress[1]) <= 31){
countMap.put("privateIp",countMap.get("privateIp")+1);
countMap.put("B",countMap.get("B")+1);
return;
}
countMap.put("B",countMap.get("B")+1);
}else if ("C".equals(type)){ //C类地址从192.0.0.0到223.255.255.255; 从192.168.0.0到192.168.255.255 C类 掩码:255.255.255.0
if ("192".equals(ipAddress[0]) && "168".equals(ipAddress[1])){
countMap.put("privateIp",countMap.get("privateIp")+1);
countMap.put("C",countMap.get("C")+1);
return;
}
countMap.put("C",countMap.get("C")+1);
}else if ("D".equals(type)){ //D类地址从224.0.0.0到239.255.255.255;
countMap.put("D",countMap.get("D")+1);
}else if ("E".equals(type)){ // E类地址从240.0.0.0到255.255.255.255
countMap.put("E",countMap.get("E")+1);
}
}
private static boolean checkMask(String[] masks){
//masks转为32位2进制字符串
StringBuilder sb = new StringBuilder();
for(int i=0;i<masks.length;i++){
masks[i] = Integer.toBinaryString(Integer.parseInt(masks[i]));
if(masks[i].length() < 8){//不足8位补齐0
for(int j=0;j < 8- masks[i].length();j++){
sb.append("0");//补完零
}
sb.append(masks[i]);//再添加转换的2进制串
}else{
sb.append(masks[i]);//刚好8位直接添加,因为之前已经判断过ip的有效性,所以不可能超过8位
}
}
//最后一个1在第一个0之前,有效,否则无效
return sb.toString().lastIndexOf("1") < sb.toString().indexOf("0");
}
private static boolean checkIp(String[] ips){
for (int i = 0; i < ips.length; i++) {
if (Integer.parseInt(ips[i]) < 0 || Integer.parseInt(ips[i]) > 255){
return false;
}
}
return true;
}
}