平时就没咋练习这些题,对ip啊,子网掩码就知道个ip协议4中配置,看到华为的这道题,说实话,题都没咋看懂。不过搞了3天后,终于搞清楚了,下面分享一些我的思路和遇到的一些坑

坑一 统计错误IP地址或错误掩码的数目

一开始我想的是,统计每一个错误的ip地址和错误掩码,举个例子,0.0.10.1~255.245.55 ,ip是错的,
子网掩码也是错的,所以算俩个,后面才发现,ip和子网掩码都错只算一个。

坑二 私有地址和公有地址是不冲突的,都算

坑三 判断子网掩码错误时,忽略了全是1和全是0的情况

坑四 2进制的方式判断子网掩码时忽略了转化后的二进制码值没有自动补齐为8位,导致判断有bug,

举个例子 255.16.0.0  255对应的是11111111 16对应的是100000 俩个直接拼接 变成11111111100000
但实际情况应该是 11111111 00100000 ,转化二进制的时候应该8位补齐。

下面分享一下思路

1、首先以行为单位接收输入的ip和子网掩码
2、其次用~分割每一行 分成ip和子网掩码
3、ip用“.”分割,这里有个小坑,“.”是split的一个关键字,必须转译一下才可以使用。split("\\.")
4. 分割后按照条件判断就好了
5. 子网掩码也是

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
List<string> strList = new ArrayList<string>();
String result ="";
while(in.hasNextLine()){
String str =in.nextLine();
strList.add(str);
result = Main.getCount(strList);
}
in.close();
System.out.println(result);
}</string></string>

public static String getCount(List<string> list){
int a =0;
int b =0;
int c =0;
int d =0;
int e =0;
int error =0;
int prit =0;</string>

for(int i=0;i<list.size();i++){
String str = list.get(i).toString();
String ip = str.substring(0,str.indexOf(""));
String dns = str.substring(str.indexOf("
")+1,str.length());
//判断子网掩码
String[] dnss = dns.split("\.");
//判断ip
String[] ips = ip.split("\.");
int ip1;
int ip2;
int ip3;
int ip4;
int dns1;
int dns2;
int dns3;
int dns4;
String dns5;
try{
ip1 = Integer.valueOf(ips[0]);
ip2 = Integer.valueOf(ips[1]);
ip3 = Integer.valueOf(ips[2]);
ip4 = Integer.valueOf(ips[3]);
dns1 = Integer.valueOf(dnss[0]);
dns2 = Integer.valueOf(dnss[1]);
dns3 = Integer.valueOf(dnss[2]);
dns4 = Integer.valueOf(dnss[3]);
dns5 = leftPade(Integer.toBinaryString(dns1))+ leftPade(Integer.toBinaryString(dns1))
+leftPade(Integer.toBinaryString(dns2))+leftPade(Integer.toBinaryString(dns3))
+leftPade(Integer.toBinaryString(dns4));

}catch(Exception f){
error += 1;
continue;
};
//A类地址1.0.0.0126.255.255.255;
// B类地址128.0.0.0
191.255.255.255;
//C类地址192.0.0.0223.255.255.255;
// D类地址224.0.0.0
239.255.255.255;
// E类地址240.0.0.0~255.255.255.255
// 私网IP范围是:
//10.0.0.0~10.255.255.255
// 172.16.0.0~172.31.255.255
//192.168.0.0~192.168.255.255
//判断子网掩码是否正确
int a1 = dns5.indexOf("1");
int b1 = dns5.lastIndexOf("1");
//子网掩码全是1 非法
if (b1 == dns5.length()-1){
error += 1;
continue;
}
//子网掩码全是0 非法
if (b1 == -1){
error += 1;
continue;
}
String tmpDns = dns5.substring(a1,b1);
int c1 = tmpDns.indexOf("0");
if(c1 != -1){
error += 1;
continue;
}

if( ip1 > 0 && ip1 < 127 ){
if(ip1 == 10){
prit +=1;
}
a += 1;
}else if(ip1 > 127 && ip1 < 192 ){
b += 1;
if(ip1 ==172&&ip2>=16&&ip2<=31){
prit +=1;
}
}else if(ip1 > 191 && ip1 < 224 ){
c +=1;
if(ip1 ==192&&ip2==168){
prit +=1;
}
}else if(ip1 > 223 && ip1 < 240 ){
d +=1;
}else if(ip1 > 239 && ip1 < 260 ){
e +=1;
}

}
return a+" "+b+" "+c+" "+d+" "+e+" "+error+" "+prit;
}
private static String leftPade(String str){
String result="";
if (str.length() < 8){
for(int i=0;i<8-str.length();i++){
result = "0"+str;
}
}else {
result = str;
}
return result;
}
}