题解 | #识别有效的IP地址和掩码并进行分类统计#

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

https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner fzhinput =  new Scanner(System.in);
        int a = 0, b = 0, c = 0, d = 0, e = 0, ff = 0, pnum = 0;
        int i;
        boolean ip, ym;
        Character zm;
        while (fzhinput.hasNextLine()) {
            String zfcs = fzhinput.nextLine();
            String dz[] = zfcs.split("~");
            if (dz.length != 2) {
                ff++;
                continue;
            }
            String ipdz[] = dz[0].split("\\.");
            String zwym[] = dz[1].split("\\.");
            if (!isValidIP(ipdz)||!isValidYM(zwym)) {
                if(Integer.parseInt(ipdz[0])==127||Integer.parseInt(ipdz[0])==0){
                    continue;
                }
                else{
                    ff++;
                    continue;
                }
                
            }
            int sz = Integer.parseInt(ipdz[0]);
            if (sz >= 1 && sz <= 126) {
                a++;
            } 
            else if (sz >= 128 && sz <= 191) {
                b++;
            } 
            else if (sz >= 192 && sz <= 223) {
                c++;
            } 
            else if (sz >= 224 && sz <= 239) {
                d++;
            } 
            else if (sz >= 240 && sz <= 255) {
                e++;
            }

            if(isPrivateIP(ipdz)){
                pnum++;
            }
        }
        System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + ff +" "+pnum);
    }
    private static boolean isValidIP(String[] ipdz) {
        int num, i;
        boolean result=true;
        if (ipdz.length != 4) {
            result=false;
        } 
        else {
            for (i = 0; i < ipdz.length; i++) {
                num = Integer.parseInt(ipdz[i]);
                if (num >= 0 && num <= 255) {

                } else {
                    result=false;
                }
            }
        }
        return result;

    }

    private static boolean isValidYM(String[] zwym) {

        int num, i;
        boolean result=true;
        StringBuilder stack = new StringBuilder();
        String erjz;
        if (zwym.length != 4) {
            result=false;
        } 
        else {
            for (i = 0; i < zwym.length; i++) {
                num = Integer.parseInt(zwym[i]);
                if (num >= 0 && num <= 255) {

                } 
                else {
                    result=false;
                }
                String zwym2jz = String.format("%8s", Integer.toBinaryString(num)).replace(' ', '0'); 
                stack.append(zwym2jz);
            }
            erjz = stack.toString();
            if (erjz.equals("11111111111111111111111111111111") || 
            erjz.equals("00000000000000000000000000000000")) {
            return false;
        }
            if(!erjz.matches("^1+0*$")){
                result=false;
            }
        }
        return result;

    }


    private static boolean isPrivateIP(String[] ipdz) {
        int num, i,sz;
        boolean result=false;
        sz = Integer.parseInt(ipdz[0]);
        if (sz==10) {
            result=true;
        } 
        else if (sz==172 && Integer.parseInt(ipdz[1])>=16 && Integer.parseInt(ipdz[1])<=31) {
            result=true;
        }
        else if (sz==192 && Integer.parseInt(ipdz[1])==168) {
            result=true;
        }
        else {
            result=false;
        }
        return result;

    }
}

全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务