题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner fzhinput = new Scanner(System.in);
boolean result = true;
String zwym;
String ip1;
String ip2;
while (fzhinput.hasNextLine()) {
result = true;
zwym = fzhinput.nextLine();
ip1 = fzhinput.nextLine();
ip2 = fzhinput.nextLine();
String ip1zu[] = ip1.split("\\.");
String ip2zu[] = ip2.split("\\.");
String zwymzu[] = zwym.split("\\.");
if (ip1zu.length != 4 || ip2zu.length != 4 || zwymzu.length != 4) {
System.out.println("1");
} else {
result = true;
StringBuilder stackzwym = new StringBuilder();
for (int j = 0; j < zwymzu.length; j++) {
int zwymsz = Integer.parseInt(zwymzu[j]);
String zwymerjz = Integer.toString(zwymsz, 2);
zwymerjz = String.format("%8s", zwymerjz).replace(" ", "0");
stackzwym.append(zwymerjz);
}
for (int i = 0; i < 4; i++) {
if (Integer.parseInt(ip1zu[i]) < 0 || Integer.parseInt(ip1zu[i]) > 255) {
result = false;
} else if (Integer.parseInt(ip2zu[i]) < 0 ||
Integer.parseInt(ip2zu[i]) > 255) {
result = false;
} else if (Integer.parseInt(zwymzu[i]) < 0 ||
Integer.parseInt(zwymzu[i]) > 255) {
result = false;
}
}
if (!stackzwym.toString().matches("^1+0*$")) {
System.out.println("1");
} else if (stackzwym.toString().matches("^1+$") ||
stackzwym.toString().matches("^0+$")) {
System.out.println("1");
} else if (result == false) {
System.out.println("1");
} else {
if (isSameIP(ip1zu, ip2zu, zwymzu)) {
System.out.println("0");
} else {
System.out.println("2");
}
}
}
}
}
private static boolean isSameIP(String[] ip1zu, String[] ip2zu,
String[] zwymzu) {
boolean pd = true;
StringBuilder stack1 = new StringBuilder();
StringBuilder stack2 = new StringBuilder();
StringBuilder stackip1 = new StringBuilder();
for (int i = 0; i < 4; i++) {
int ipsz1 = Integer.parseInt(ip1zu[i]);
String ip1erjz = Integer.toString(ipsz1, 2);
ip1erjz = String.format("%8s", ip1erjz).replace(" ", "0");
stackip1.append(ip1erjz);
stackip1.append(" ");
}
String ip1 = stackip1.toString();
StringBuilder stackip2 = new StringBuilder();
for (int i = 0; i < 4; i++) {
int ipsz2 = Integer.parseInt(ip2zu[i]);
String ip2erjz = Integer.toString(ipsz2, 2);
ip2erjz = String.format("%8s", ip2erjz).replace(" ", "0");
stackip2.append(ip2erjz);
stackip2.append(" ");
}
String ip2 = stackip2.toString();
StringBuilder stackzwym = new StringBuilder();
for (int i = 0; i < 4; i++) {
int zwymsz = Integer.parseInt(zwymzu[i]);
String zwymerjz = Integer.toString(zwymsz, 2);
zwymerjz = String.format("%8s", zwymerjz).replace(" ", "0");
stackzwym.append(zwymerjz);
stackzwym.append(" ");
}
String zwym = stackzwym.toString();
for (int i = 0; i < zwym.length(); i++) {
if (ip1.charAt(i) == zwym.charAt(i)) {
stack1.append(ip1.charAt(i));
} else {
stack1.append('0');
}
}
for (int i = 0; i < zwym.length(); i++) {
if (ip2.charAt(i) == zwym.charAt(i)) {
stack2.append(ip2.charAt(i));
} else {
stack2.append('0');
}
}
if (stack1.toString().equals(stack2.toString())) {
pd = true;
} else {
pd = false;
}
return pd;
}
}

