识别有效的IP地址和掩码JAVA版
识别有效的IP地址和掩码并进行分类统计
http://www.nowcoder.com/questionTerminal/de538edd6f7e4bc3a5689723a7435682
JAVA版
显然需要首选判断掩码是否合法,如果合法,再去判断IP。否则直接错误类++;
判断掩码思路:通过split()方法将掩码划分为4段,注意此处有个坑,即点符号需要转义"\."。之后定义一个方法,将十进制的数转化为二进制,再作为字符串输出。将这四段字符串连成一个,通过indexof("0")和Lastindexof("1")的大小关系判断是否前面全为1,后面全为0。注意此处有两个坑,第一个坑是定义的转化二进制数字的方法中,高位的0是不会添加的,需要另外添加补成8位。比如16,定义的方法转化为10000,我们需要补成00010000。第二个坑是在于掩码中有的位置为空,比如255.255..0,使用split("\.")只能分成三段,会产生数组越界异常,因此分割完后判断数组长度是否为4,不是4直接计入错误类,此坑同样会出现在后续的判断IP中。
判断IP思路:如果掩码合法,才需判断IP。第一层if判断第一段数字,第二层if判断后三段数字,此外第二层if后面还需跟个if判断是否为私网ip。第一层if中,不符合ABCDE类的不能直接判为错误类,还得判断是否是【0...】和【127...】类的IP地址。
PS:代码看起来长,其实很多重复代码,没高兴优化
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int[] count=new int[7]; while (sc.hasNext()){ String str=sc.nextLine(); String[] IPandYM=str.split("~"); String[] YMarray=IPandYM[1].split("\\."); String[] IParray=IPandYM[0].split("\\."); boolean YMisWrong=false; String YMstr=""; for (int i=0;i<4;i++){ YMstr=YMstr+getBinary(Integer.valueOf(YMarray[i])); } if (YMstr.indexOf("0")==-1||YMstr.indexOf("1")==-1||YMstr.indexOf("0")<YMstr.lastIndexOf("1") ||IParray.length!=4||YMarray.length!=4){ YMisWrong=true; count[5]++; } if (!YMisWrong){ if(IParray[0]!=""&&Integer.valueOf(IParray[0])>=1&&Integer.valueOf(IParray[0])<=126){ if(IParray[1]!=""&&IParray[2]!=""&&IParray[3]!=""&& Integer.valueOf(IParray[1])>=0&&Integer.valueOf(IParray[1])<=255&& Integer.valueOf(IParray[2])>=0&&Integer.valueOf(IParray[2])<=255&& Integer.valueOf(IParray[3])>=0&&Integer.valueOf(IParray[3])<=255){ count[0]++; if (Integer.valueOf(IParray[0])==10) count[6]++; } }else if(IParray[0]!=""&&Integer.valueOf(IParray[0])>=128&&Integer.valueOf(IParray[0])<=191){ if(IParray[1]!=""&&IParray[2]!=""&&IParray[3]!=""&& Integer.valueOf(IParray[1])>=0&&Integer.valueOf(IParray[1])<=255&& Integer.valueOf(IParray[2])>=0&&Integer.valueOf(IParray[2])<=255&& Integer.valueOf(IParray[3])>=0&&Integer.valueOf(IParray[3])<=255){ count[1]++; if (Integer.valueOf(IParray[0])==172&&Integer.valueOf(IParray[1])>=16&&Integer.valueOf(IParray[1])<=31) count[6]++; } }else if(IParray[0]!=""&&Integer.valueOf(IParray[0])>=192&&Integer.valueOf(IParray[0])<=223){ if(IParray[1]!=""&&IParray[2]!=""&&IParray[3]!=""&& Integer.valueOf(IParray[1])>=0&&Integer.valueOf(IParray[1])<=255&& Integer.valueOf(IParray[2])>=0&&Integer.valueOf(IParray[2])<=255&& Integer.valueOf(IParray[3])>=0&&Integer.valueOf(IParray[3])<=255){ count[2]++; if (Integer.valueOf(IParray[0])==192&&Integer.valueOf(IParray[1])==168) count[6]++; } }else if(IParray[0]!=""&&Integer.valueOf(IParray[0])>=224&&Integer.valueOf(IParray[0])<=239){ if(IParray[1]!=""&&IParray[2]!=""&&IParray[3]!=""&& Integer.valueOf(IParray[1])>=0&&Integer.valueOf(IParray[1])<=255&& Integer.valueOf(IParray[2])>=0&&Integer.valueOf(IParray[2])<=255&& Integer.valueOf(IParray[3])>=0&&Integer.valueOf(IParray[3])<=255){ count[3]++; } }else if(IParray[0]!=""&&Integer.valueOf(IParray[0])>=240&&Integer.valueOf(IParray[0])<=255){ if(IParray[1]!=""&&IParray[2]!=""&&IParray[3]!=""&& Integer.valueOf(IParray[1])>=0&&Integer.valueOf(IParray[1])<=255&& Integer.valueOf(IParray[2])>=0&&Integer.valueOf(IParray[2])<=255&& Integer.valueOf(IParray[3])>=0&&Integer.valueOf(IParray[3])<=255){ count[4]++; } }else{ if (Integer.valueOf(IParray[0])!=0&&Integer.valueOf(IParray[0])!=127){ count[5]++; } } } } System.out.println(count[0]+" "+count[1]+" "+count[2]+" "+count[3]+" "+count[4]+" "+count[5]+" "+count[6]); } public static String getBinary(int num) { int currentNum = num;//存放当前的被除数 LinkedList<String> list = new LinkedList<String>();//存放余数,也是就二进制数 if (num==0){ return "00000000"; } while (currentNum != 0) { if (currentNum % 2 == 0) { list.addFirst("0"); } else { list.addFirst("1"); } currentNum /= 2; } StringBuilder sb = new StringBuilder();//当然你可以使用其他形式作为方法的返回 if (list.size()<8){ for (int i = 0; i < 8 - list.size(); i++) { sb.append("0"); } } for (int i = 0; i < list.size(); i++) { sb.append(list.get(i)); } return sb.toString();