题解 | #识别有效的IP地址和掩码并进行分类统计#
识别有效的IP地址和掩码并进行分类统计
http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
import java.util.*;
public class Main {
private static int aCodeNum;
private static int bCodeNum;
private static int cCodeNum;
private static int dCodeNum;
private static int eCodeNum;
private static int errorCodeNum;
private static int privateCodeNum;
public static boolean isVaildip(String ip){
try{
String[] ipi = ip.split("\\.");
if(ipi.length != 4){
return false;
}
for(String ss:ipi){
if(Integer.parseInt(ss) < 0 || Integer.parseInt(ss) > 255){
return false;
}
}
}catch(Exception e){
return false;
}
return true;
}
public static boolean isVaildMask(String mask){
if(!isVaildip(mask)){
return false;
}
String[] maski = mask.split("\\.");
//将mask的每个网络号和主机号转化为2进制串,不足8位的前面补
StringBuilder sb = new StringBuilder();
for(String ss:maski){
String binary = Integer.toBinaryString(Integer.parseInt(ss));
// binary = String.format("%08d",Integer.parseInt(binary));//不足的8位的补齐
if(binary.length()<8){
for(int j=0;j<8-binary.length();j++){
sb.append("0");
}
}
sb.append(binary);
}
//最后一个1在第一个0之前,index逐渐变大:11111111111111111111111100000000
boolean flag = sb.toString().lastIndexOf("1") < sb.toString().indexOf("0");
return flag;
}
public static boolean getCodeNum(String code,String ziCode){
if(code.startsWith("0.") || code.startsWith("127.")){
return true;
}
if(!isVaildip(code) || !isVaildMask(ziCode)){
errorCodeNum++;
return false;
}
String[] strs = code.split("\\.");
int privateNum = 0;
for(int i=0;i<strs.length;i++){
privateNum = 0;
if(i==0 && (strs[i].equals("0") || strs[i].equals("127"))){
continue;
}
int ins = Integer.parseInt(strs[i]);
if(i==0){
if(ins>=1 && ins<=126){
aCodeNum++;
if(ins==10){
privateCodeNum++;
}
}
if(ins>=128&&ins<=191){
bCodeNum++;
if(ins==172){
privateNum = 172;
}
}
if(ins>=192 && ins<=223){
cCodeNum++;
if(ins==192){
privateNum = 192;
}
}
if(ins>=224 && ins<=239){
dCodeNum++;
}
if(ins>=240 && ins<=255){
eCodeNum++;
}
}
if(i==1){
if(privateNum==172 && ins>=16 && ins<=31){
++privateCodeNum;
}
if(privateNum==192 && ins==168){
++privateCodeNum;
}
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String next = sc.nextLine();
String[] strs = next.split("\\~");
getCodeNum(strs[0],strs[1]);
}
System.out.println(aCodeNum +" "+bCodeNum+" "+cCodeNum+" "+dCodeNum+" "+eCodeNum+" "+errorCodeNum+" "+privateCodeNum);
}
}
public class Main {
private static int aCodeNum;
private static int bCodeNum;
private static int cCodeNum;
private static int dCodeNum;
private static int eCodeNum;
private static int errorCodeNum;
private static int privateCodeNum;
public static boolean isVaildip(String ip){
try{
String[] ipi = ip.split("\\.");
if(ipi.length != 4){
return false;
}
for(String ss:ipi){
if(Integer.parseInt(ss) < 0 || Integer.parseInt(ss) > 255){
return false;
}
}
}catch(Exception e){
return false;
}
return true;
}
public static boolean isVaildMask(String mask){
if(!isVaildip(mask)){
return false;
}
String[] maski = mask.split("\\.");
//将mask的每个网络号和主机号转化为2进制串,不足8位的前面补
StringBuilder sb = new StringBuilder();
for(String ss:maski){
String binary = Integer.toBinaryString(Integer.parseInt(ss));
// binary = String.format("%08d",Integer.parseInt(binary));//不足的8位的补齐
if(binary.length()<8){
for(int j=0;j<8-binary.length();j++){
sb.append("0");
}
}
sb.append(binary);
}
//最后一个1在第一个0之前,index逐渐变大:11111111111111111111111100000000
boolean flag = sb.toString().lastIndexOf("1") < sb.toString().indexOf("0");
return flag;
}
public static boolean getCodeNum(String code,String ziCode){
if(code.startsWith("0.") || code.startsWith("127.")){
return true;
}
if(!isVaildip(code) || !isVaildMask(ziCode)){
errorCodeNum++;
return false;
}
String[] strs = code.split("\\.");
int privateNum = 0;
for(int i=0;i<strs.length;i++){
privateNum = 0;
if(i==0 && (strs[i].equals("0") || strs[i].equals("127"))){
continue;
}
int ins = Integer.parseInt(strs[i]);
if(i==0){
if(ins>=1 && ins<=126){
aCodeNum++;
if(ins==10){
privateCodeNum++;
}
}
if(ins>=128&&ins<=191){
bCodeNum++;
if(ins==172){
privateNum = 172;
}
}
if(ins>=192 && ins<=223){
cCodeNum++;
if(ins==192){
privateNum = 192;
}
}
if(ins>=224 && ins<=239){
dCodeNum++;
}
if(ins>=240 && ins<=255){
eCodeNum++;
}
}
if(i==1){
if(privateNum==172 && ins>=16 && ins<=31){
++privateCodeNum;
}
if(privateNum==192 && ins==168){
++privateCodeNum;
}
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String next = sc.nextLine();
String[] strs = next.split("\\~");
getCodeNum(strs[0],strs[1]);
}
System.out.println(aCodeNum +" "+bCodeNum+" "+cCodeNum+" "+dCodeNum+" "+eCodeNum+" "+errorCodeNum+" "+privateCodeNum);
}
}