题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
http://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =new Scanner(System.in); String dns =input.nextLine(); String ip1=input.nextLine(); String ip2=input.nextLine(); String[] dns_str=dns.split("\\."); String[] ip1_str=ip1.split("\\."); String[] ip2_str=ip2.split("\\."); //无效 boolean valid_dns=true; String s=""; for (int i = 0; i < dns_str.length; i++) { String string = dns_str[i]; if (!(Integer.valueOf(string)>=0&&Integer.valueOf(string)<=255)) { valid_dns=false; break; }else { if (Integer.valueOf(string)!=0) { int n=Integer.valueOf(string); String string2=""; for (int j = 0; n/2>0; j++) { string2=n%2+string2; n=n/2; } string2="1"+string2; int m=string2.length(); if (string2.length()<8) { for (int j = 0; j < 8-m; j++) { string2="0"+string2; } } s=s+string2; }else { s=s+"00000000"; } } } // System.out.println(s); int count0=0; int count1=0; for (int j = 0; j < s.length(); j++) { if (s.charAt(j)=='0') { count0=j; break; } } for (int j = s.length()-1; j >=0; j--) { if (s.charAt(j)=='1') { count1=j; break; } } // System.out.println(count1+" "+count0); if (count1>count0) { valid_dns=false; } boolean valid_ip1=true; for (int i = 0; i < ip1_str.length; i++) { String string = ip1_str[i]; if (!(Integer.valueOf(string)>=0&&Integer.valueOf(string)<=255)) { valid_ip1=false; break; } } boolean valid_ip2=true; for (int i = 0; i < ip2_str.length; i++) { String string = ip2_str[i]; if (!(Integer.valueOf(string)>=0&&Integer.valueOf(string)<=255)) { valid_ip2=false; break; } } if (valid_ip2==false|| valid_ip1==false||valid_dns==false) { System.out.println(1); return; } //有效 String dns_two=""; for (int i = 0; i < dns_str.length; i++) { String string = dns_str[i]; if (Integer.valueOf(string)!=0) { int n=Integer.valueOf(string); String string2=""; for (int j = 0; n/2>0; j++) { string2=n%2+string2; n=n/2; } string2="1"+string2; int m=string2.length(); if (string2.length()<8) { for (int j = 0; j < 8-m; j++) { string2="0"+string2; } } dns_two=dns_two+string2; }else { dns_two=dns_two+"00000000"; } } // System.out.println(dns_two); String ip1_two=""; for (int i = 0; i < ip1_str.length; i++) { String string = ip1_str[i]; if (Integer.valueOf(string)!=0) { int n=Integer.valueOf(string); String string2=""; for (int j = 0; n/2>0; j++) { string2=n%2+string2; n=n/2; } string2="1"+string2; int m=string2.length(); if (string2.length()<8) { for (int j = 0; j < 8-m; j++) { string2="0"+string2; } } ip1_two=ip1_two+string2; }else { ip1_two=ip1_two+"00000000"; } } // System.out.println(ip1_two); String ip2_two=""; for (int i = 0; i < ip2_str.length; i++) { String string = ip2_str[i]; if (Integer.valueOf(string)!=0) { int n=Integer.valueOf(string); String string2=""; for (int j = 0; n/2>0; j++) { string2=n%2+string2; n=n/2; } string2="1"+string2; int m=string2.length(); if (string2.length()<8) { for (int j = 0; j < 8-m; j++) { string2="0"+string2; } } ip2_two=ip2_two+string2; }else { ip2_two=ip2_two+"00000000"; } } // System.out.println(ip2_two); int count=0; for (int i = dns_two.length()-1; i >=0; i--) { if (dns_two.charAt(i)=='1') { count=i; break; } } // System.out.println(count); if (ip1_two.substring(0,count).equals(ip2_two.substring(0,count))) { System.out.println(0); } else { System.out.println(2); } } }