话不多说,暴力求解就完事儿了。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { // 注意 while 处理多个 case
String s1 = sc.nextLine();
String ip1 = sc.nextLine();
String ip2 = sc.nextLine();
int[]ip00 = getNum(s1);
int[]ip11 = getNum(ip1);
int[]ip22 = getNum(ip2);
if(valid(ip00)&&valid(ip11)&&valid(ip22)){
int[]ip000 = transform(ip00);
int flag = 0;
for(int i =0;i<31;++i){
if(ip000[i]==0&&ip000[i+1]==1) {
System.out.println(1);
flag = 1;
break;
}
}
if(flag==1)continue;
int[]ip111 = transform(ip11);
int[]ip222 = transform(ip22);
if(check(ip000,ip111,ip222)){
System.out.println(0);
}else System.out.println(2);
}
else System.out.println(1);
}
}
public static boolean valid000(int []ip){
for(int i =0;i<31;++i){
if(ip[i]==0&&ip[i+1]==1)return false;
}
return true;
}
public static boolean check(int[]ip1,int[]ip2,int[]ip3){
int [] ipcheck1 = new int[32];
int []ipcheck2 = new int[32];
for(int i = 0;i<32;++i){
ipcheck1[i] = ip1[i]&ip2[i];
ipcheck2[i] = ip1[i]&ip3[i];
if(ipcheck1[i]!=ipcheck2[i])return false;
}
return true;
}
public static int[] transform(int[]ips){
int index = 0;
int []res = new int[32];
for(int i =0;i<ips.length;++i){
int [] temp = getBit(ips[i]);
for(int j = 0;j<8;++j){
res[index++] = temp[j];
}
}
return res;
}
public static int[] getBit(int n){
int res[] = new int[8];
int index = 7;
while(n!=0){
res[index--] = n%2;
n/=2;
}
return res;
}
public static boolean valid(int[]ips){
if(ips.length!=4)return false;
for(int i:ips){
if(i>=0&&i<256)continue;
else return false;
}
return true;
}
public static int[] getNum(String ip){
String []ips = ip.split("\\.");
int []res = new int[ips.length];
int index = 0;
for(String s:ips){
res[index++] = Integer.parseInt(s);
}
return res;
}
}