牛客网-华为在线编程题-1

牛客网-华为在线编程题-1

请解析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。(例如:255.255.255.32就是一个非法的掩码) 
本题暂时默认以0开头的IP地址是合法的,比如0.1.1.2,是合法地址

输入描述: 
多行字符串。每行一个IP地址和掩码,用~隔开。 
输出描述: 
统计A、B、C、D、E、错误IP地址或错误掩码、私有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

输出例子: 
1 0 1 0 0 2 1

代码:: 
C++: 
1.

#include <stdio.h>

int main()
{

    int A=0,B=0,C=0,D=0,E=0,W=0,S=0,i,j,k,P,Q;


    int Num[8]={-1,-1,-1,-1,-1,-1,-1,-1};

    while(scanf("%d.%d.%d.%d~%d.%d.%d.%d",&Num[0],&Num[1],&Num[2],&Num[3],&Num[4],&Num[5],&Num[6],&Num[7])!=EOF)
    {
        int flag1=0,flag2=0;
        for(i=0;i<8;i++)
        {
            if(Num[i]<0&&Num[i]>255)
            {
                  W++;
                  flag2=1;
                  break;
            }
        }
        if(Num[4]==255&&Num[5]==255&&Num[6]==255&&Num[7]==255||Num[4]==0)
        {
            W++;
            flag2=1;
        }
        if(flag2==0)
        {
            for(i=4;i<8;i++)
            {
                if(Num[i]!=255)
                {
                    if(Num[i]<128&&Num[i]!=0)
                    {
                        W++;
                        flag1=1;
                        break;
                    }
                    for(j=i+1;j<8;j++)
                        if(Num[j]!=0)
                        {
                            W++;
                            flag1=1;
                            break;
                        }
                        break;
                }
            }
             if(flag1==0)
             {
                    P=Num[i];
                    j=0;
                    while(P>0)
                    {

                        Q=P%2;
                        if(Q==1)
                            flag2=1;
                        if(Q==0&&flag2==1)
                        {
                            W++;
                            flag1=1;
                            break;
                        }
                         P=P/2;
                    }              
            }
            if(flag1==0)
            {
               if(Num[0]>=1&&Num[0]<=126)
                   A++;
               if(Num[0]>=128&&Num[0]<=191)
                   B++;
                if(Num[0]>=192&&Num[0]<=223)
                   C++;
                if(Num[0]>=224&&Num[0]<=239)
                   D++;
                if(Num[0]>=240&&Num[0]<=255)
                   E++;
                if(Num[0]==10)
                   S++;
                if(Num[0]==172&&Num[1]<=31&&Num[1]>=16)
                   S++;
                if(Num[0]==192&&Num[1]==168)
                   S++;
            }
        }
        for(i=0;i<8;i++)
            Num[i]=-1;
    }  
    printf("%d %d %d %d %d %d %d",A,B,C,D,E,W,S);   
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

java. 
1:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int a=0,b=0,c=0,d=0,e=0,f=0,g=0;
        while(in.hasNext()){
            int flag=0;
            String str=in.nextLine();
            String[] s=str.split("~");
            String[] ss1=s[0].split("\\.");
            String[] ss2=s[1].split("\\.");
            if(ss1.length!=4||ss2.length!=4){
                f++;
                continue;
            }
            for(int i=0;i<4;i++)
            {
                if(Integer.parseInt(ss1[i])<0||Integer.parseInt(ss1[i])>255)
                {f++;break;}
            }
            int mask=Integer.parseInt(ss2[0]);
            for(int i=1;i<4;i++)
            {
                mask=mask<<8;
                mask=mask+Integer.parseInt(ss2[i]);
            }
            if((mask-1|mask)!=0xffffffff||mask==0xffffffff)
                {f++;flag=1;}
            int ss=Integer.parseInt(ss1[0]);
            int s1=Integer.parseInt(ss1[1]);
            if(ss>=1&&ss<=126&&flag==0)
                {
                a++;
                if(ss==10&&flag==0)
                g++;
            }
            else if(ss>=128&&ss<=191&&flag==0)
                {
                    b++;
                    if(ss==172&&s1>=16&&s1<=31)
                    {
                        g++;
                    }
            }

            else if(ss>=192&&ss<=223&&flag==0)
                {
                c++;
                if(ss==192&&s1==168)
                    g++;
            }
            else if(ss>=224&&ss<=239&&flag==0)
                d++;
            else if(ss>=240&&ss<=255&&flag==0)
                e++;

        }
        System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f+" "+g);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

2:

import java.util.*;
public class Main {

    public static List correctMask = Arrays.asList("255", "254", "252", "248", "240", "224", "192", "128");

    public static boolean isMaskValid(String mask) {

        if ("0.0.0.0".equals(mask) || "255.255.255.255".equals(mask)) {
            return false;
        }

        String[] maskSeg = mask.split("\\.");

        for (int i = maskSeg.length - 1; i >= 0; i--) {
            if (correctMask.contains(maskSeg[i])) {
                for (int j = i - 1; j >= 0; j--) {
                    if (!"255".equals(maskSeg[j])) {
                        return false;
                    }
                }
                break;
            } else if ("0".equals(maskSeg[i])) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] res = new int[7];
        while (in.hasNext()) {

            String str = in.next();
//          if ("esc".equals(str)) {
//              break;
//          }
            String[] s = str.split("~");

            String ip = s[0];
            String mask = s[1];

//          System.out.println("ip: " + ip);
//          System.out.println("mask: " + mask);

            //判断掩码是否合法
            boolean isMaskValid = isMaskValid(mask);
            if (!isMaskValid) {
//              System.out.println("mask: " + mask);
                res[5] ++;
                continue;
            }

            String[] ipSeg = ip.split("\\.");
//          for (String ss : ipSeg) {
//              System.out.println(ss);
//          }
            for (int i = 0; i < ipSeg.length; i++) { //检查IP是否合法
                if (ipSeg[i].length() == 0 || Integer.parseInt(ipSeg[i]) > 255) {
//                  System.out.println("ip: " + ip);
                    res[5] ++;
                    continue;
                }
            }
            int firstSeg = Integer.parseInt(ipSeg[0]);
            if (firstSeg >= 1 && firstSeg <= 126) {
                res[0] ++;
                if (firstSeg == 10) {
                    res[6] ++;
                }
            } else if (firstSeg == 127) {
                int secondSeg = Integer.parseInt(ipSeg[1]);
                if (secondSeg >= 16 && secondSeg <= 31) {
                    res[6] ++;
                }
            } else if (firstSeg >= 128 && firstSeg <= 191) {
                res[1] ++;
            }else if (firstSeg >= 192 && firstSeg <= 223) {
                res[2] ++;
                if (Integer.parseInt(ipSeg[1]) == 168) {
                    res[6]++;
                }
            } else if (firstSeg >= 224 && firstSeg <= 239) {
                res[3] ++;
            } else if (firstSeg >= 240 && firstSeg <= 255) {
                res[4] ++;
            } //else {
            //  System.out.println("ip: " + ip);
            //  res[5] ++;
            //}

        }
        for (int i = 0; i < res.length - 1; i++) {
            System.out.print(res[i] + " ");
        }
        System.out.println(res[res.length - 1]);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

3:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int typeA = 0;
        int typeB = 0;
        int typeC = 0;
        int typeD = 0;
        int typeE = 0;
        int errorIpOrMaskCode = 0;
        int privIp = 0;

        while (scanner.hasNext()) {
            String ipt = scanner.nextLine();
            String[] ipAndMaskCode = ipt.split("~");
            String ip = ipAndMaskCode[0];
            String maskCode = ipAndMaskCode[1];
            // 判断格式
            if (!isValidFormat(ip) || !isValidFormat(maskCode)) {
                errorIpOrMaskCode++;
                continue;
            }

            // 判断掩码是否错误
            if (!validMaskCode(maskCode)) {
                errorIpOrMaskCode++;
                continue;
            }

            // 判断ip类别
            String fnStr = ip.substring(0, ip.indexOf("."));
            int fn = Integer.valueOf(fnStr);
            if (fn >= 1 && fn < 127) {
                // A
                typeA++;
            } else if (fn >= 128 && fn < 192) {
                // B
                typeB++;
            } else if (fn >= 192 && fn < 224) {
                // C
                typeC++;
            } else if (fn >= 224 && fn < 240) {
                // D
                typeD++;
            } else if (fn >= 240 && fn <= 255) {
                // E
                typeE++;
            }

            // 判断是否是私网IP
            String ipSubStr = ip.substring(ip.indexOf(".") + 1);
            String snStr = ipSubStr.substring(0, ipSubStr.indexOf("."));
            int sn = Integer.valueOf(snStr);
            if (fn == 10 || (fn == 172 && sn >= 16 && sn <= 31) || (fn == 192 && sn == 168)) {
                privIp++;
            }
//          System.out.printf("%d %d%n", fn, sn);

        }
        scanner.close();

        System.out.printf("%d %d %d %d %d %d %d%n", typeA, typeB, typeC, typeD, typeE, errorIpOrMaskCode, privIp);

    }

    /**
     * 判断ip和掩码是否是xxx.xxx.xxx.xxx格式Ø
     *
     * @param ip
     * @return
     */
    private static boolean isValidFormat(String ip) {
        boolean res = true;
        if (ip == null || "".equals(ip))
            return false;
        Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$");
        Matcher matcher = pattern.matcher(ip);

        if (matcher.matches()) {
            String[] nums = ip.split("\\.");
            for (String num : nums) {
                int n = Integer.valueOf(num);
                if (n < 0 || n > 255) {
                    res = false;
                    break;
                }
            }
        } else {
            res = false;
        }

        return res;
    }

    /**
     * 判断掩码是否是前面全为1后面全为0 的格式
     *
     * @param maskCode
     * @return
     */
    private static boolean validMaskCode(String maskCode) {
        boolean res = true;
        String[] nums = maskCode.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (String num : nums) {
            int n = Integer.valueOf(num);
            sb.append(binaryString(n));
        }
        int firstIndexOf0 = sb.indexOf("0");
        int lastIndexOf1 = sb.lastIndexOf("1");
        if (firstIndexOf0 < lastIndexOf1) {
            res = false;
        }
        return res;
    }

    /**
     * 将整数转成对应的八位二进制字符串
     * @param num
     * @return
     */
    private static String binaryString(int num) {
        StringBuilder result = new StringBuilder();
        int flag = 1 << 7;
        for (int i = 0; i < 8; i++) {
            int val = (flag & num) == 0 ? 0 : 1;
            result.append(val);
            num <<= 1;
        }
        return result.toString();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137

C#. 
1:

using System;
using System.Text;
using System.Text.RegularExpressions;
public class Solution
{
    public static void Main(string[] args)
    {
        string line;
        string[] p;
        string ip, maskCode;
        int[] res = new int[7];
        while((line = Console.ReadLine())!=null)
        {
            p = line.Split('~');
            ip = p[0];
            maskCode = p[1];
            // 判断格式
            if (!isValidFormat(ip) || !isValidFormat(maskCode)) {
                res[6]++;
                continue;
            }

            // 判断掩码是否错误
            if (!ValidMaskCode(maskCode)) {
                res[6]++;
                continue;
            }

            // 判断ip类别
            string fnStr = ip.Substring(0, ip.IndexOf("."));
            int fn = int.Parse(fnStr);
            if (fn >= 1 && fn < 127) {
                // A
                res[0]++;
            } else if (fn >= 128 && fn < 192) {
                // B
                res[1]++;
            } else if (fn >= 192 && fn < 224) {
                // C
                res[2]++;
            } else if (fn >= 224 && fn < 240) {
                // D
                res[3]++;
            } else if (fn >= 240 && fn <= 255) {
                // E
                res[4]++;
            }

            // 判断是否是私网IP
            String ipSubStr = ip.Substring(0,ip.IndexOf('.') + 1);
            string snStr = ipSubStr.Substring(0, ipSubStr.IndexOf('.'));
            int sn = int.Parse(snStr);
            if (fn == 10 || (fn == 172 && sn >= 16 && sn <= 31) || (fn == 192 && sn == 168)) {
                res[5]++;
            }
            //         

        }
           Console.WriteLine("{0} {1} {2} {3} {4} {5} {6}",
                              res[0],res[1],res[2],res[3],res[4],res[6],res[5]);

    }


    /**
     * 判断掩码是否是前面全为1后面全为0 的格式
     *
     * @param maskCode
     * @return
     */
    private static bool ValidMaskCode(String maskCode)
    {
        bool res = true;
        String[] nums = maskCode.Split('.');
        StringBuilder sb = new StringBuilder();
        foreach (String num in nums) {
            int n = int.Parse(num);
            sb.Append(binaryString(n));
        }
        string cur = sb.ToString();
        int firstIndexOf0 = cur.IndexOf("0");
        int lastIndexOf1 = cur.LastIndexOf("1");
        if (firstIndexOf0 < lastIndexOf1)
        {
            res = false;
        }
        return res;
    }

    /**
         * 将整数转成对应的八位二进制字符串
         * @param num
         * @return
         */
    private static String binaryString(int num)
    {
        StringBuilder result = new StringBuilder();
        int flag = 1 << 7;
        for (int i = 0; i < 8; i++)
        {
            int val = (flag & num) == 0 ? 0 : 1;
            result.Append(val);
            num <<= 1;
        }
        return result.ToString();
    }


    /**
       * 判断ip和掩码是否是xxx.xxx.xxx.xxx格式Ø
       * @param ip
       * @return
       */
    private static bool isValidFormat(String ip)
    {
        bool res = true;
        if (ip == null || ip.Equals(""))
            return false;
        string pattern = @"^(\d+)\.(\d+)\.(\d+)\.(\d+)$";

        if(Regex.IsMatch(ip, pattern))
        {
            String[] nums = ip.Split('.');
            foreach (String num in nums)
            {
                int n = int.Parse(num);
                if (n < 0 || n > 255)
                {
                    res = false;
                    break;
                }
            }
        }
        else
        {
            res = false;
        }

        return res;
    }



}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值