"Huawei OJ" "075-determine whether two IPs belong to the same subnet"

Source: Internet
Author: User

"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "075-Determine whether two IP belongs to the same subnet" project download "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: IP address 192.168.0.1 Subnet mask 255.255.255.0 conversion to binary: IP address 11010000.10101000.00000000.00000001 Subnet mask 1111         1111.11111111.11111111.00000000AND operation 11000000.10101000.00000000.00000000 converted to decimal: 192.168.0.0IP address 192.168.0.254 Subnet mask 255.255.255.0 conversion to binary: IP address 11010000.10101000.00000000.11111110 Subnet mask 1111111 1.11111111.11111111.00000000AND operation 11000000.10101000.00000000.00000000 converted to decimal after: 192.168.0.0 through After the and operation of the IP address of the two computers and the subnet mask, 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. /** * Function: To determine the IP address of the two computers is the same sub-network. * Input Parameters: string mask: netmask, Format: "255.255.255.0"; * String ip1: Computer 1 IP address, format: "192.168.0.254"; * String IP2 : Computer 2 IP address, format: "192.168.0.1"; * * return value: 0:ip1 and IP2 belong to the same subnet; * 1:IP address or subnet mask format is illegal; * 2:ip1 and IP2 do not belong to the same subnet */public int ChecknetSegment (String mask, String ip1, String ip2) {/* Implements function here */return 0;} 
Enter a description
输入子网掩码、两个ip地址
Output description
得到计算结果
Input example
255.255.255.0192.168.224.256192.168.10.4
Output example
1
Algorithm implementation
ImportJava.util.Scanner;/** * Author: Wang Junshu * date:2016-01-03 12:59 * declaration:all rights Reserved!!! */ Public  class Main {     Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt"));         while(Scanner.hasnext ())            {String subnet = Scanner.next ();            String ip1 = Scanner.next ();            String ip2 = Scanner.next ();        System.out.println (subnetjudgement (subnet, ip1, ip2));    } scanner.close (); }Private Static int subnetjudgement(string subnet, String ip1, String ip2) {//1:IP address or subnet mask format is illegal        if(!ipvalidate (subnet) | |!ipvalidate (IP1) | |!ipvalidate (IP2)) {return 1; }intSubnetint = Ipstrtoint (subnet);//1: Subnet mask format is illegal        if(!subnetmaskvalidate (Subnetint)) {return 1; }intb = Ipstrtoint (IP2);intA = Ipstrtoint (IP1);//0:ip1 and IP2 belong to the same sub-network        if((A & subnetint) = = (b & subnetint)) {return 0; }//2:ip1 and IP2 do not belong to the same subnet        Else{return 2; }    }/** * Verify that the IP address is in the correct format * * @param IP IP address * @return true: Format correct, false: Malformed */    Private Static Boolean ipvalidate(String IP) {string[] part = Ip.split ("\\.");//if (part.length! = 4) {//return false;//        }         for(String S:part) {Try{intnum = Integer.parseint (s);if(Num <0|| num >255) {return false; }            }Catch(Exception ex) {return false; }        }return true; }/** * Subnet mask verification, the network number section is all "1" and the host number part is all "0" * * @param IP * @return  */    Private Static Boolean subnetmaskvalidate(intIP) {BooleanHaszero =false;intand =0x80000000; while(And! =0) {//The bit position being processed is 0            if(IP & and) = =0) {//Description appeared 0Haszero =true; }//If location is 1            Else{///Before 0 has occurred, that means 1 is discontinuous, so the subnet mask is not legal                if(Haszero) {return false; }            }//Unsigned Right Move oneand >>>=1; }return true; }/** * Converts a dotted decimal IP address into an integer representation * * @param ip dot decimal IP address * @return An integer table of IP addresses * /    Private Static int Ipstrtoint(String IP) {string[] part = Ip.split ("\\.");intIntip =0; for(inti =0; i < part.length; i++) {intt = Integer.parseint (part[i]); Intip + = t << ( --8* i); }returnIntip; }}

"Huawei OJ" "075-determine whether two IPs belong to the same subnet"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.