• 识别有效的IP地址和掩码并进行分类统计


    描述

    请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。

    所有的IP地址划分为 A,B,C,D,E五类

    A类地址1.0.0.0~126.255.255.255; 

    B类地址128.0.0.0~191.255.255.255; 

    C类地址192.0.0.0~223.255.255.255;

    D类地址224.0.0.0~239.255.255.255;

    E类地址240.0.0.0~255.255.255.255

    私网IP范围是:

    10.0.0.0~10.255.255.255

    172.16.0.0~172.31.255.255

    192.168.0.0~192.168.255.255

    子网掩码为前面是连续的1,然后全是0

    知识点 字符串,循环,查找,搜索,排序,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归
    运行时间限制 0M
    内存限制 0
    输入

    多行字符串。每行一个IP地址和掩码,已~隔开。如:

    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

    输出

    统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开,根据上面的IP,可以得到:

    1.0.0.1~255.0.0.0 ----A类

    192.168.0.2~255.255.255.0  ----C类,私有

    10.70.44.68~255.254.255.0----错误的掩码

    19..0.~255.255.255.0-----错误的IP

    可以得到统计数据如下:

    1 0 1 0 0 2 1

    样例输入 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
    样例输出 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,countPrivate = 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]<0&&iips[i]>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]<0&&imasks[i]>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);
    	}
    }
    

      

    态度决定高度,细节决定成败,
  • 相关阅读:
    tensor张量
    Image Stride(内存图像行跨度)
    Batch Normalization
    论文阅读
    codeforces 520B
    codeforces 467B
    C语言位运算
    codeforces 474D
    codeforces 545c
    codeforces 698A
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/5343976.html
Copyright © 2020-2023  润新知