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

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

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

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    int countA = 0;
    int countB = 0;
    int countC = 0;
    int countD = 0;
    int countE = 0;
    int wrongCount = 0;
    int privateIp = 0;
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
        String path = sc.nextLine();
        String[] strs = path.split("~");
        String ip = strs[0];
        String code = strs[1];
        String[] ips = ip.split("\\.");
        String[] codes = code.split("\\.");
        if (ips.length != 4 || codes.length != 4) {
            wrongCount++;
            continue;
        }
        if ((Integer.parseInt(ips[0]) >= 1 && Integer.parseInt(ips[0]) <= 126)
                && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
            countA++;
                if ((Integer.parseInt(ips[0]) == 10)
                        && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                        privateIp++;
                        continue;
                }
                if ((Integer.parseInt(ips[0]) == 172)
                        && (Integer.parseInt(ips[1]) >= 16 && Integer.parseInt(ips[1]) <= 31)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                        privateIp++;
                        continue;
                }
                if ((Integer.parseInt(ips[0]) == 192)
                        && (Integer.parseInt(ips[1]) == 168)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                        privateIp++;
                        continue;
                }
                continue;
            }else {
                wrongCount++;
                continue;
            }



        }
        if ((Integer.parseInt(ips[0]) >= 128 && Integer.parseInt(ips[0]) <= 191)
                && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                countB++;
                if ((Integer.parseInt(ips[0]) == 10)
                        && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 172)
                        && (Integer.parseInt(ips[1]) >= 16 && Integer.parseInt(ips[1]) <= 31)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 192)
                        && (Integer.parseInt(ips[1]) == 168)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                continue;
            }else {
                wrongCount++;
                continue;
            }

        }
        if ((Integer.parseInt(ips[0]) >= 192 && Integer.parseInt(ips[0]) <= 223)
                && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                countC++;
                if ((Integer.parseInt(ips[0]) == 10)
                        && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 172)
                        && (Integer.parseInt(ips[1]) >= 16 && Integer.parseInt(ips[1]) <= 31)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 192)
                        && (Integer.parseInt(ips[1]) == 168)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                continue;
            }else {
                wrongCount++;
                continue;
            }

        }
        if ((Integer.parseInt(ips[0]) >= 224 && Integer.parseInt(ips[0]) <= 239)
                && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {

            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                countD++;
                if ((Integer.parseInt(ips[0]) == 10)
                        && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 172)
                        && (Integer.parseInt(ips[1]) >= 16 && Integer.parseInt(ips[1]) <= 31)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 192)
                        && (Integer.parseInt(ips[1]) == 168)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                continue;
            }else {
                wrongCount++;
                continue;
            }

        }
        if ((Integer.parseInt(ips[0]) >= 240 && Integer.parseInt(ips[0]) <= 255)
                && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                countE++;
                if ((Integer.parseInt(ips[0]) == 10)
                        && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 172)
                        && (Integer.parseInt(ips[1]) >= 16 && Integer.parseInt(ips[1]) <= 31)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                if ((Integer.parseInt(ips[0]) == 192)
                        && (Integer.parseInt(ips[1]) == 168)
                        && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                        && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
                    privateIp++;
                    continue;
                }
                continue;
            }else {
                wrongCount++;
                continue;
            }

        }
        if ((Integer.parseInt(ips[0]) == 10)
                && (Integer.parseInt(ips[1]) >= 0 && Integer.parseInt(ips[1]) <= 255)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                privateIp++;
                continue;
            }else {
                wrongCount++;
                continue;
            }
        }
        if ((Integer.parseInt(ips[0]) == 172)
                && (Integer.parseInt(ips[1]) >= 16 && Integer.parseInt(ips[1]) <= 31)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                privateIp++;
                continue;
            }else {
                wrongCount++;
                continue;
            }
        }
        if ((Integer.parseInt(ips[0]) == 192)
                && (Integer.parseInt(ips[1]) == 168)
                && (Integer.parseInt(ips[2]) >= 0 && Integer.parseInt(ips[2]) <= 255)
                && (Integer.parseInt(ips[3]) >= 0 && Integer.parseInt(ips[3]) <= 255)) {
            String binarySunbet = "";
            for(int i = 0; i < 4;i++){
                int num = Integer.parseInt(codes[i]);
                String str = Integer.toBinaryString(num);
                while(str.length() < 8){
                    str = "0"+str;
                }
                binarySunbet += str;
            }
            if(binarySunbet.toString().lastIndexOf("1") < binarySunbet.toString().indexOf("0")){
                privateIp++;
                continue;
            }else {
                wrongCount++;
                continue;
            }
        }
        if ((Integer.parseInt(ips[0]) == 0) || (Integer.parseInt(ips[0]) == 127)) {
            //不做处理
            continue;
        }
        wrongCount++;
    }
        System.out.println(countA + " " + countB + " " + countC + " " + countD + " " + countE + " " + wrongCount + " " + privateIp);
}

}

全部评论

相关推荐

07-11 11:10
门头沟学院 Java
请问各位大三兄弟们跟hr说多久实习时间到时候可以提前跑路吗?
程序员小白条:问就是六个月以上,可以一年,实习都这样,你入职后想跑就跑
点赞 评论 收藏
分享
07-10 14:08
已编辑
江西农业大学 Java
念旧select:做完把项目放到自己硬盘里给他看,看完拷走
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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