题解 | #判断两个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);
        }
		
	}

}

全部评论

相关推荐

03-30 21:02
已编辑
武汉大学 Java
ALEX_BLX:虽然说聊天记录不可信,不过这个趋势确实如此但我觉得也要想到一点就是卷后端的人里真正有“料”的人又有多少,我说的这个料都不是说一定要到大佬那种级别,而是就一个正常的水平。即使是现在也有很多人是跟风转码的,2-3个月速成后端技术栈的人数不胜数,但今时不同往日没可能靠速成进大厂了。这种情况就跟考研一样,你能上考场就已经打败一半的人了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务