import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int A, B, C, D, E, F, I; A = 0;//A、B、C、D、E类IP B = 0; C = 0; D = 0; E = 0; F = 0;//F是False的数量 I = 0;//I是私有IP的数量 while (in.hasNextLine()) { String str; str = in.nextLine(); String[] sp = str.split("~"); if (sp.length != 2) { F++; break; }//按照波浪号分割 String[] ip1 = sp[0].split("\\.");//按照.来分割,.是特殊字符,需要用\\来转义 String[] son1 = sp[1].split("\\."); int[] ip = new int[4]; int[] son = new int[4]; if (ip1.length == 4 && son1.length == 4) {//防止有缺漏,有的没有没有四个 for (int i = 0; i < ip1.length; i++)ip[i] = Integer.parseInt(ip1[i]); for (int i = 0; i < son1.length; i++)son[i] = Integer.parseInt(son1[i]);//将IP和子网掩码son用Integer.parseInt转换成int类型 //ip判定 //接下来是一个隐藏难点,子网掩码转换成二进制要在前面补0,不满八位补成八位 if (son[0] >= 0 && son[0] <= 255 && son[1] >= 0 && son[1] <= 255 && son[2] >= 0 && son[2] <= 255 && son[3] >= 0 && son[3] <= 255) { String bit1 = Integer.toBinaryString(son[0]) ; String bit; for (int i = 0; bit1.length() + i < 8; i++) { bit1 = "0" + bit1; } bit = bit1; bit1 = Integer.toBinaryString(son[1]) ; for (int i = 0; bit1.length() + i < 8; i++) { bit1 = "0" + bit1; } bit = bit + bit1; bit1 = Integer.toBinaryString(son[2]) ; for (int i = 0; bit1.length() + i < 8; i++) { bit1 = "0" + bit1; } bit = bit + bit1; bit1 = Integer.toBinaryString(son[3]) ; for (int i = 0; bit1.length() + i < 8; i++) { bit1 = "0" + bit1; } bit = bit + bit1; //补完之后判断子网掩码是否合规,合规则t=0,继续判断IP;否则t!=0,如果ip不是1/127则F++,错误加一 int j = 0, t = 0; for (int i = 0; i < bit.length(); i++) { if (bit.charAt(0) == '0' || bit.charAt(bit.length() - 1) == '1') { t++; if (ip[0] != 0 && ip[0] != 127) { F++; } break; } if (bit.charAt(i) == '0') { j = i + 1; break; } } if (j != 0) { for (; j < bit.length(); j++) { if (bit.charAt(j) == '1') { if (ip[0] != 0 && ip[0] != 127) F++; t++; break; } } } ipcompare://这一段是ip比较,达成任意一个条件都可以跳出判断,如过是特殊字符的ip直接F++ if (t == 0) { if (ip[0] == 0 || ip[0] == 127) { break ipcompare; } if (ip[0] >= 1 && ip[0] <= 126) { if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)A++; else F++; } else if (ip[0] >= 128 && ip[0] <= 191) { if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)B++; else F++; } else if (ip[0] >= 192 && ip[0] <= 223) { if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)C++; else F++; } else if (ip[0] >= 224 && ip[0] <= 239) { if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)D++; else F++; } else if (ip[0] >= 240 && ip[0] <= 255) { if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)E++; else F++; } else F++; //这一点是判断是否是私有ip if (ip[0] == 10) { if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)I++; } if (ip[0] == 172) { if (ip[1] >= 16 && ip[1] <= 31 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)I++; } if (ip[0] == 192) { if (ip[1] == 168 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3] <= 255)I++; } } } else {//无论子网掩码是否是符合的数字,都要判断是否是1.127的开头ip ip[0] = Integer.parseInt(ip1[0]); if (ip[0] != 0 && ip[0] != 127) F++; } } else {//无论ip和子网是否长度合规,也都判断一下 ip[0] = Integer.parseInt(ip1[0]); if (ip[0] != 0 && ip[0] != 127) F++; } } //输出结果 System.out.print(A); System.out.print(' '); System.out.print(B); System.out.print(' '); System.out.print(C); System.out.print(' '); System.out.print(D); System.out.print(' '); System.out.print(E); System.out.print(' '); System.out.print(F); System.out.print(' '); System.out.print(I); } }
这题难点比较杂
先定义要输出的几个参数
int A, B, C, D, E, F, I;
A = 0;//A、B、C、D、E类IP
B = 0;
C = 0;
D = 0;
E = 0;
F = 0;//F是False的数量
I = 0;//I是私有IP的数量
首先接收到字符串需要进行分割,分割成俩个字符串
String str;
str = in.nextLine();
String[] sp = str.split("~");
if (sp.length != 2) {
F++;
break;
}//按照波浪号分割
其次是每个字符串要接着按照.进行分割
坑点一:.在java中不是可以直接进行分割的,要加入\\进行转义
String[] ip1 = sp[0].split("\\.");//按照.来分割,.是特殊字符,需要用\\来转义
String[] son1 = sp[1].split("\\.");
之后是因为ip要比较的范围是整数型,还要将之转化为整数
坑点二:因为有1..0.,这样在进行分割之后并不会填满4个,长度会短,这样在后面会产生越界
int[] ip = new int[4];
int[] son = new int[4];
if (ip1.length == 4 && son1.length == 4) {//防止有缺漏,有的没有没有四个
for (int i = 0; i < ip1.length; i++)ip[i] = Integer.parseInt(ip1[i]);
for (int i = 0; i < son1.length; i++)son[i] = Integer.parseInt(son1[i]);
之后还要将子网掩码改成二进制型,这里一定要注意,子网掩码都是八位数,所以要补0
坑点三:子网掩码转化为二进制之后,在前面补0,先判断int型son里面数字是否是0-255之间
if (son[0] >= 0 && son[0] <= 255 && son[1] >= 0 && son[1] <= 255 &&
son[2] >= 0 && son[2] <= 255 && son[3] >= 0 &&
son[3] <= 255) {
String bit1 = Integer.toBinaryString(son[0]) ;
String bit;
for (int i = 0; bit1.length() + i < 8; i++) {
bit1 = "0" + bit1;
}
bit = bit1;
bit1 = Integer.toBinaryString(son[1]) ;
for (int i = 0; bit1.length() + i < 8; i++) {
bit1 = "0" + bit1;
}
bit = bit + bit1;
bit1 = Integer.toBinaryString(son[2]) ;
for (int i = 0; bit1.length() + i < 8; i++) {
bit1 = "0" + bit1;
}
bit = bit + bit1;
bit1 = Integer.toBinaryString(son[3]) ;
for (int i = 0; bit1.length() + i < 8; i++) {
bit1 = "0" + bit1;
}
bit = bit + bit1;
之后就是进行判断掩码是否合规
//补完之后判断子网掩码是否合规,合规则t=0,继续判断IP;否则t!=0,如果ip不是1/127则F++,错误加一
int j = 0, t = 0;
for (int i = 0; i < bit.length(); i++) {
if (bit.charAt(0) == '0' || bit.charAt(bit.length() - 1) == '1') {
t++;
if (ip[0] != 0 && ip[0] != 127) {
F++;
}
break;
}
if (bit.charAt(i) == '0') {
j = i + 1;
break;
}
}
if (j != 0) {
for (; j < bit.length(); j++) {
if (bit.charAt(j) == '1') {
if (ip[0] != 0 && ip[0] != 127) F++;
t++;
break;
}
}
}
ipcompare://这一段是ip比较,达成任意一个条件都可以跳出判断,如过是特殊字符的ip直接F++
子网掩码合规之后再进行判断ip
坑点四,ip为0和127的,都不算在违规里面,不能算F++
if (t == 0) {
if (ip[0] == 0 || ip[0] == 127) {
break ipcompare;
}
if (ip[0] >= 1 && ip[0] <= 126) {
if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)A++;
else F++;
} else if (ip[0] >= 128 && ip[0] <= 191) {
if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)B++;
else F++;
} else if (ip[0] >= 192 && ip[0] <= 223) {
if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)C++;
else F++;
} else if (ip[0] >= 224 && ip[0] <= 239) {
if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)D++;
else F++;
} else if (ip[0] >= 240 && ip[0] <= 255) {
if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)E++;
else F++;
} else F++;
//这一点是判断是否是私有ip
if (ip[0] == 10) {
if (ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)I++;
}
if (ip[0] == 172) {
if (ip[1] >= 16 && ip[1] <= 31 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)I++;
}
if (ip[0] == 192) {
if (ip[1] == 168 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 &&
ip[3] <= 255)I++;
}
}
} else {//无论子网掩码是否是符合的数字,都要判断是否是1.127的开头ip
ip[0] = Integer.parseInt(ip1[0]);
if (ip[0] != 0 && ip[0] != 127) F++;
}
} else {//无论ip和子网是否长度合规,也都判断一下
ip[0] = Integer.parseInt(ip1[0]);
if (ip[0] != 0 && ip[0] != 127) F++;
}
}
最后输出System.out.print(A);
System.out.print(' ');
System.out.print(B);
System.out.print(' ');
System.out.print(C);
System.out.print(' ');
System.out.print(D);
System.out.print(' ');
System.out.print(E);
System.out.print(' ');
System.out.print(F);
System.out.print(' ');
System.out.print(I);