Title Description
A subnet mask is a basis for determining whether the IP addresses of any two computers belong to the same subnet.
The subnet mask is the same as the IP address structure, which is a 32-bit binary number, where the network number section is all "1" and the host number is all "0". Use the subnet mask to determine whether two hosts are in the same subnet. If the IP addresses of the two hosts are the same as the result of their subnet mask, respectively, the two hosts are in the same subnet.
Example:
I P Address 192.168.0.1
Subnet Mask 255.255.255.0
Convert to binary for operation:
I P Address 11010000.10101000.00000000.00000001
Subnet Mask 11111111.11111111.11111111.00000000
and operations
11000000.10101000.00000000.00000000
After conversion to decimal:
192.168.0.0
I P Address 192.168.0.254
Subnet Mask 255.255.255.0
Convert to binary for operation:
I P Address 11010000.10101000.00000000.11111110
Subnet Mask 11111111.11111111.11111111.00000000
and operations
11000000.10101000.00000000.00000000
After conversion to decimal:
192.168.0.0
By using the and operation of the IP address of the two computers and the subnet mask above, we can see that the result of the operation is the same. are 192.168.0.0, so these two computers can be considered to be the same subnet.
Input Description:
Enter a subnet mask, two IP addresses
Output Description:
Get calculated results
Input Example:
255.255.255.0192.168.224.256192.168.10.4
Output Example:
1
Import java.util.*;p Ublic class Main {public static void main (string[] args) {Scanner scan=new Scanner (system.in); String[] Str1=scan.nextline (). split ("\ \"); String[] Str2=scan.nextline (). split ("\ \"); String[] Str3=scan.nextline (). split ("\ \"); if (Yu (STR1,STR2). Equals ("1")) {System.out.println ("1");} else if (Yu (STR1,STR3). Equals ("1")) {System.out.println ("1");} else if (Yu (str1,str2). Equals (Yu (STR1,STR3))) {System.out.println ("0");} Else{system.out.println ("2");}} Two number and static String Yu (string[] str1,string[] str2) {int result=0; StringBuilder sb=new StringBuilder (); for (int i=0;i<4;i++) {if (Integer.parseint (Str1[i]) <0 | | Integer.parseint (Str1[i]) >255) {result=1;} else if (Integer.parseint (Str2[i]) <0 | | Integer.parseint (Str2[i]) >255) {result=1;} else{result= (Integer.parseint (Str1[i]) &integer.parseint (Str2[i]); Sb.append (result); if (i!=3) sb.append (".");}} if (result!=0) {return "1";} Else{return sb.tostring ();}}}
Huawei oj--determine whether two IPs belong to the same subnet