Identify valid IP addresses and masks and classify statistics

Source: Internet
Author: User

Describe

Please parse the IP address and the corresponding mask for classification. Requirements are categorized according to the A/b/c/d/e class address, and illegal addresses and masks are categorized separately.

All IP addresses are classified as A,b,c,d,e five classes

Class A address 1.0.0.0~126.255.255.255;

Class B address 128.0.0.0~191.255.255.255;

Class C address 192.0.0.0~223.255.255.255;

Class D address 224.0.0.0~239.255.255.255;

Class E Address 240.0.0.0~255.255.255.255

The IP range of the private network is:

10.0.0.0~10.255.255.255

172.16.0.0~172.31.255.255

192.168.0.0~192.168.255.255

The subnet mask is contiguous 1 for the front, and then it's all 0.

Knowledge points String, loop, find, search, sort, function, pointer, enumeration, bitwise operation, struct, union, file operation, recursive
Run time limit 0M
Memory limit 0
Input

Multi-line string. One IP address and mask per line, separated by ~. Such as:

10.70.44.68~255.254.255.0

1.0.0.1~255.0.0.0

192.168.0.2~255.255.255.0

19..0.~255.255.255.0

Output

Statistics A, B, C, D, E, error IP address or error mask, the number of private IP, separated by a space, according to the above IP, you can get:

1.0.0.1~255.0.0.0----Class A

192.168.0.2~255.255.255.0----Class C, private

10.70.44.68~255.254.255.0----the wrong mask

19..0.~255.255.255.0-----The wrong IP

Statistics can be obtained as follows:

1 0 1 0 0 2 1

Sample input 10.70.44.68~255.254.255.0 1.0.0.1~255.0.0.0 192.168.0.2~255.255.255.0 19..0.~255.255.255.0
Sample output 1 0 1 0 0 2 1
Package Com.oj;import Java.util.scanner;public class Testoj {private static Boolean judge (Int[] mask) {StringBuilder Masks = new StringBuilder (); for (int i = 0;i < mask.length; i++) masks.append (integer.tobinarystring (Mask[i]));// System.out.println (Masks.tostring ()); String str = new String (masks.tostring ()), for (int i = 0;i < Str.length ()-1; i++) if (Str.charat (i) = = ' 0 ' && Str.charat (i+1) = = ' 1 ') return False;return true;} public static void Main (string[] args) {int CountA = 0,COUNTB = 0,COUNTC = 0,countd = 0,counte = 0,counterror = 0,countpri vate = 0; Scanner in = new Scanner (system.in), while (In.hasnext ()) {String str = in.nextline (); string[] data = Str.split ("~"); String IP = data[0]; String mask = data[1]; string[] ips = ip.split ("\ \"); string[] Masks = mask.split ("\ \"); if (ips.length!=4&&masks.length!=4) {counterror++;//system.out.println ("Error"); continue;} Boolean flag = false;int[] Iips = new Int[4];for (int i = 0;i < 4; i++) {Try{iips[i] = Integer.parseint (ips[i]); if (iips[i ]&LT;0&AMP;&AMP;IIPS[I]&GT;255) flag = true;} catch (NumberFormatException e) {flag = True;break;}} if (flag) {counterror++;//system.out.println ("Error"); continue;} Flag = False;int [] imasks = new Int[4];for (int i = 0;i < 4; i++) {Try{imasks[i] = Integer.parseint (masks[i]); if (imasks[ I]&LT;0&AMP;&AMP;IMASKS[I]&GT;255) flag = true;} catch (NumberFormatException e) {flag = True;break;}} if (flag) {counterror++;//system.out.println ("Error"); continue;} if (!judge (imasks)) {counterror++;//system.out.println ("Error"); continue;} if (iips[0]<=126&&iips[0]>=1) {counta++;//system.out.println ("A"); if (iips[0]==10) countPrivate++; Continue;} else if (iips[0]>=128&&iips[0]<=191) {countb++;if (iips[1]>=16&&iips[1]<=31) Countprivate++;//system.out.println ("B"); continue;} else if (iips[0]>=192&&iips[0]<=223) {countc++;if (iips[1]==168) countprivate++;//system.out.println ( "C"); continue;} else if (iips[0]>=224&&iips[0]<=239) {countd++;//system.out.println ("D"); continue;}else if (iips[0]>=240&&iips[0]<=255) {counte++;//system.out.println ("E"); continue;}} System.out.println (counta+ "" +countb+ "" +countc+ "" +countd+ "" +counte+ "" +counterror+ "" +countprivate ";}}

  

Identify valid IP addresses and masks and classify statistics

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.