本题将会给出
条地址信息,确切数字未知,您需要一直读取至文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每条地址信息描述如下:
每行输入一个
形式的 IP 地址和一个
形式的子网掩码,中间用波浪线(
)分隔。保证
要么为空,要么是一个
到
间的整数。
在一行上输出七个整数,分别代表 A 类地址数、B 类地址数、C 类地址数、D 类地址数、E 类地址数、错误 IP 或错误子网掩码数、私有 IP 数。
10.70.44.68~1.1.1.5 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
在这个样例中:
第一条地址信息:掩码非法;
第二条地址信息:IP 格式和掩码均合法,属于 A 类;
第三条地址信息:IP 格式和掩码均合法,属于 C 类私有地址;
第四条地址信息:IP 格式非法。
统计得到
个 A 类,
个 B 类,
个 C 类,
个 D 类,
个 E 类,
个错误条目,
个私有地址。
0.201.56.50~255.255.255.0 127.201.56.50~255.255.111.255
0 0 0 0 0 0 0
在这个样例中,两条地址信息均属于上方提示中提到的特殊 IP 地址,不需要处理,直接跳过。特别需要注意地,第二条地址的子网掩码是非法的。但是因为该条为特殊 IP 地址,此优先级更高,所以不进入统计。
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-30 更新题面。
2. 2024-12-16 更新题面。
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
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, err = 0, pri = 0;
while (in.hasNextLine()) {
String s = in.nextLine();
String s1[] = s.split("~");
int[] ip = transToInt(s1[0]);
if (ip[0] == 0 || ip[0] == 127) {
continue;
}
int[] sonIp = transToInt(s1[1]);
if (!judgeErrIp(ip) || !judgeSonIp(sonIp)) {
err++;
continue;
}
if (judgePriIp(ip)) {
pri++;
}
if (ip[0] < 128) {
a++;
} else if (ip[0] < 192) {
b++;
} else if (ip[0] < 224) {
c++;
} else if (ip[0] < 240) {
d++;
} else {
e++;
}
}
System.out.print(a + " " + b + " " + c + " " + d + " " + e + " " + err + " " +
pri);
}
private static boolean judgeErrIp(int[] ip) {
if (ip.length != 4) {
return false;
}
for (int i : ip) {
if ("".equals(i) || i > 255) {
return false;
}
}
return true;
}
private static boolean judgeSonIp(int[] ip){
if(ip.length != 4){
return false;
}
boolean b = true;
int sum255 = 0;
for(int i : ip){
if(i == 255){
sum255++;
}
if("".equals(i) || i>255){
return false;
}
if(b && i<255){
String numBinary = Integer.toBinaryString(i);
String[] t = numBinary.split("0");
if(t.length > 1){
return false;
}
b = false;
}
if(!b && i>0){
return false;
}
}
return sum255==4 ? false:true;
}
private static boolean judgePriIp(int[] ip) {
if (ip[0] == 10) {
return true;
} else if (ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31) {
return true;
} else if (ip[0] == 192 && ip[1] == 168) {
return true;
} else {
return false;
}
}
private static int[] transToInt(String s) {
String[] s1 = s.split("\\.");
int[] arr = new int[s1.length];
for (int i = 0; i < s1.length; i++) {
if ("".equals(s1[i])) {
arr[i] = -1;
continue;
}
arr[i] = Integer.valueOf(s1[i]);
}
return arr;
}
} 0 0 0 0 0 0 0正确的代码逻辑是:
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 0-4 : A-E
// 5-7 : pri
long[][] range = {
{ ParseIP("1.0.0.0"), ParseIP("126.255.255.255")},
{ ParseIP("128.0.0.0"), ParseIP("191.255.255.255")},
{ ParseIP("192.0.0.0"), ParseIP("223.255.255.255")},
{ ParseIP("224.0.0.0"), ParseIP("239.255.255.255")},
{ ParseIP("240.0.0.0"), ParseIP("255.255.255.255")},
};
long[][] private_range = {
{ ParseIP("10.0.0.0"), ParseIP("10.255.255.255")},
{ ParseIP("172.16.0.0"), ParseIP("172.31.255.255")},
{ ParseIP("192.168.0.0"), ParseIP("192.168.255.255")}
};
int[] result = new int[7];
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String input = in.nextLine();
String ip = input.split("~")[0];
String mark = input.split("~")[1];
long ip_bin = ParseIP(ip);
long mark_bin = ParseIP(mark);
if (IsIgnore(ip)) {
continue;
}
if (!IsMarkValid(mark_bin)){
result[5]++;
continue;
}
if (ip_bin == -1) {
result[5]++;
continue;
}
for (int i = 0; i < range.length; i++) {
if (ip_bin >= range[i][0] && ip_bin <= range[i][1]) {
result[i]++;
break;
}
}
for (int i = 0; i < private_range.length; i++) {
if (ip_bin >= private_range[i][0] && ip_bin <= private_range[i][1]) {
result[6]++;
break;
}
}
}
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
static long ParseIP(String str) {
String[] list = str.split("\\.");
if (list.length != 4) {
return -1;
}
for (int i = 0; i < list.length; i++) {
if (list[i] == "") {
return -1;
}
}
long result = 0;
for (int i = 0; i < 4; i++) {
result |= Long.parseLong(list[i]) << ((3 - i) * 8);
}
return result;
}
static boolean IsMarkValid(long mark) {
boolean isZero = true;
long m = 1;
if ((m & mark) != 0) {
return false; // all 1
}
for (int i = 0; i < 32; i++) {
if (!isZero && (m & mark) == 0) {
return false;
}
if ((m & mark) != 0) {
isZero = false;
}
m <<= 1;
}
if (isZero == true) {
return false; // all 0
}
return true;
}
static boolean IsIgnore(String str) {
String[] list = str.split("\\.");
return list[0].equals("127") || list[0].equals("0");
}
} import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
// while (in.hasNextInt()) { // 注意 while 处理多个 case
// int a = in.nextInt();
// int b = in.nextInt();
// System.out.println(a + b);
// }
int[] result = new int[7];
while (in.hasNextLine()) {
String[] data = in.nextLine().split("~");
//check ip 是否非法, 掩码是否非法
if(!checkFormat(data)){
result[5]++ ;
continue;
}
//check 是哪一类
int checkTypeResult = checkType(data[0]);
if(checkTypeResult != -1){
result[checkTypeResult]++;
}
//check 是否是私有
result[6] += checkPrivate(data[0]);
}
System.out.print(result[0]);
for(int i=1; i<result.length; i++){
System.out.print(" ");
System.out.print(result[i]);
}
}
public static boolean checkFormat(String[] data) {
return checkIp(data[0]) && checkMask(data[1]);
}
public static boolean checkIp(String ip) {
String[] fields = ip.split("[.]");
if (fields.length != 4) {
return false;
}
for (int i = 0; i < 4; i++) {
int value = 0;
try {
value = Integer.parseInt(fields[i]);
} catch (Exception e) {
return false;
}
if (value < 0 || value > 255) {
return false;
}
}
return true;
}
public static boolean checkMask(String mask) {
String[] fields = mask.split("[.]");
if (mask.equals("255.255.255.255") || mask.equals("0.0.0.0") || fields.length != 4) {
return false;
}
int flag = 0;
for (int i = 3; i >=0; i--) {
int value = 0;
try {
value = Integer.parseInt(fields[i]);
} catch (Exception e) {
return false;
}
for(int j =0; j<8; j++){
int bit = value%2;
flag = flag | bit;
if(flag == 1 && bit==0){
return false;
}
value = value >> 1;
}
}
return true;
}
public static int checkType(String ip) {
int first = Integer.parseInt(ip.split("[.]")[0]);
if(first>=1 && first<=126){
return 0;
}else if(first>=128 && first<=191){
return 1;
}else if(first>=192 && first<=223){
return 2;
}else if(first>=224 && first<=239){
return 3;
}else if(first>=240 && first<=255){
return 4;
}else{
return -1;
}
}
public static int checkPrivate(String ip) {
String[] fields = ip.split("[.]");
boolean firstCase = fields[0].equals("10");
boolean secondCase = fields[0].equals("172") && Integer.parseInt(fields[1])<=31;
boolean thirdCase = fields[0].equals("192") && fields[1].equals("168");
return (firstCase || secondCase || thirdCase)? 1: 0;
}
} import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static int[] ipCount = new int[5];
static int privateIdCount = 0 ;
static int errors = 0 ;
static String sMac = ",0,128,192,224,240,248,252,254,255,";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String ip = in.nextLine();
ipIsLegal(ip.split("~")[0], ip.split("~")[1]);
}
System.out.print(
ipCount[0] + " " + ipCount[1] + " " + ipCount[2] + " " + ipCount[3] + " " +
ipCount[4] + " " + errors + " " + privateIdCount
);
}
/**
* 判断ip是否合法
*/
private static void ipIsLegal(String ip, String mac) {
String regex =
"([1-9]?\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.([1-9]?\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$";
;
if (ip.startsWith("0") || ip.startsWith("127")) {
return;
}
if (!Pattern.matches(regex, ip) || !macIsLegal(mac) || mac.endsWith("255") ||
mac.startsWith("0") || ip.split("\\.").length != 4) {
errors ++ ;
return;
};
int ip0 = Integer.parseInt(ip.split("\\.")[0]);
int ip1 = Integer.parseInt(ip.split("\\.")[1]);
if (0 < ip0 && ip0 < 127) {
if (10 == ip0) {
privateIdCount++;
}
ipCount[0] += 1;
} else if (127 < ip0 && ip0 < 192) {
if (172 == ip0 && (15 < ip1 && ip1 < 32)) {
privateIdCount++;
}
ipCount[1] += 1;
} else if (192 <= ip0 && ip0 < 224) {
if (192 == ip0 && 168 == ip1) {
privateIdCount++;
}
ipCount[2] += 1;
} else if (224 <= ip0 && ip0 < 240) {
ipCount[3] += 1;
} else if (240 <= ip0 && ip0 < 256) {
ipCount[4] += 1;
}
}
/**
判断子网是否合法
*/
private static boolean macIsLegal(String mac) {
String[] ss = mac.split("\\.");
if (ss.length != 4) {
return false;
}
for (String s : ss) {
if ("".equals(s) || !sMac.contains(","+s+",")) {
return false;
}
}
return Arrays.stream(ss).map(s1->Integer.toBinaryString(Integer.parseInt(
s1))).collect(Collectors.joining("")).replaceAll("1+", "1").replaceAll("0+",
"0").length() == 2;
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int aIP = 0,bIP = 0,cIP = 0,dIP = 0,eIP = 0,wrongIP = 0,privateIP = 0;
while(sc.hasNext()) {
boolean flag = true;//标识是否错误IP,用来跳过其他校验
String input = sc.nextLine();
String ip = input.split("~")[0];
String subnet = input.split("~")[1];
if(subnet.equals("255.255.255.255") || subnet.equals("0.0.0.0")) {
wrongIP++;
flag=false;
}
String[] subnetUnit = subnet.split("\\.");
String[] ips = ip.split("\\.");
if(subnetUnit.length!=4 || ips.length!=4) {
wrongIP++;
flag = false;
}
if(ips[0].equals("0") || ips[0].equals("127")){
continue;
}
if(flag) {
for(int i=3; i>=0; i--) {
if(Integer.valueOf(subnetUnit[i])>255 || Integer.valueOf(subnetUnit[i])<0) {
wrongIP++;
flag = false;
}
if(Integer.valueOf(ips[i])>255 || Integer.valueOf(ips[i])<0) {
wrongIP++;
flag = false;
}
}
}
if(flag && !isMask(subnetUnit)) {
wrongIP++;
flag = false;
}
if(flag) {
for(int i=3; i>=0; i--) {
if(ips[i].equals("")) {
wrongIP++;
flag = false;
break;
}
}
}
if(flag) {
if(ips[0].equals("192") && ips[1].equals("168")) privateIP++;
if(ips[0].equals("172") && Integer.valueOf(ips[1])>=16 && Integer.valueOf(ips[1])<=31) privateIP++;
if(ips[0].equals("10")) privateIP++;
if(Integer.valueOf(ips[0])>=240) eIP++;
else if(Integer.valueOf(ips[0])>=224) dIP++;
else if(Integer.valueOf(ips[0])>=192) cIP++;
else if(Integer.valueOf(ips[0])>=128) bIP++;
else if(Integer.valueOf(ips[0])>=1 && Integer.valueOf(ips[0])<127) aIP++;
}
}
System.out.println(aIP+" "+bIP+" "+cIP+" "+dIP+" "+eIP+" "+wrongIP+" "+privateIP);
}
public static boolean isMask(String[] subnetUnit) {
String[] rightSubnet = new String[]{"128","192","224","240","248","252","254","255"};
for(int i=3; i>=0; i--) {
if(rightSubnet[i].equals("")) return false;
if(subnetUnit[i].equals("0")) continue;
else if(i==0) {
if(!Arrays.asList(rightSubnet).contains(subnetUnit[i]))
return false;
} else {
if(!Arrays.asList(rightSubnet).contains(subnetUnit[i])) {
return false;
}
for(int j=0; j<i; j++) {
if(!subnetUnit[j].equals("255"))
return false;
}
}
}
return true;
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。
// 【0.*.*.*】和【127.*.*.*】
int a=0,b=0,c=0,d=0,e=0,f=0,s=0;
while (in.hasNext()) { // while 处理多个 case
//获取ip地址,掩码用字符串数组ips和yms存储
String str = in.nextLine();
String [] strs = str.split("~");
String[] ips =strs[0].split("\\.");
String[] yms =strs[1].split("\\.");
//过滤掉非法长度IP地址
if(ips.length!=4){f++;}
//过滤掉0.*.*.*】和【127.*.*.*】的IP地址
else if(!ips[0].equals("0")&&!ips[0].equals("127")){
boolean bl =true; //表示合法IP地址
//是否为合法IP地址
for (int i = 0; i < 4; i++) {
int t =Integer.parseInt(ips[i]);
if(t<0||t>255){f++;bl=false;break;}
}
//是否为合法掩码
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int ym =Integer.parseInt(yms[i]);
for (int j = 7; j >= 0; j--) {
if ((ym&(1<<j))>0){sb.append("1");}else {sb.append("0");}
}
}
int count0=0;
int count1=0;
for (int i = 0; i < sb.length(); i++) {
if(sb.charAt(i)=='0')count0++;
if(sb.charAt(i)=='1')count1++;
if((i!=sb.length()-1)&&sb.charAt(i)<sb.charAt(i+1)){f++;bl=false;break;}
}
if(count0==sb.length()||count1==sb.length()){f++;bl=false;}
//合法IP地址属于哪一类
if(bl){
int t =Integer.parseInt(ips[0]);
int v =Integer.parseInt(ips[1]);
if(t<=126&&t>=1){a++;if(t==10)s++;}
else if(t<=191&&t>=127){b++;if(t==172&&v==16)s++;}
else if(t<=223&&t>=192){c++;if(t==192&&v==168)s++;}
else if(t<=239&&t>=224){d++;}
else if(t<=255&&t>=240){e++;}
}
}
}
//输出
System.out.print(a+" ");
System.out.print(b+" ");
System.out.print(c+" ");
System.out.print(d+" ");
System.out.print(e+" ");
System.out.print(f+" ");
System.out.print(s);
}
} import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { //A,B,C,D,E,错误IP地址或错误掩码,私有IP的个数 //0,1,2,3,4,5 ,6 int[] result = new int[7]; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String next = null; // 注意 hasNext 和 hasNextLine 的区别 while ((next = br.readLine()) != null) { if ("over".equals(next)) { break; } String[] ipAndYanma = next.split("~"); String[] ip = ipAndYanma[0].split("\\."); String[] netmask = ipAndYanma[1].split("\\."); int[] rightMask = new int[8]; rightMask[7] = 0; rightMask[0] = 128; rightMask[1] = 192; rightMask[2] = 224; rightMask[3] = 240; rightMask[4] = 248; rightMask[5] = 252; rightMask[6] = 254; // StringBuilder rightMask = new StringBuilder("128 192 224 240 248 252 254"); //判断IP是否正确 if (validateIp(ip, result)) continue; //排除类似于【0.*.*.*】和【127.*.*.*】的IP地址 if (checkPrivate(ip)) continue; //子网掩码合法性判断 if (validateNetMask(netmask, result, rightMask)) continue; //A B C D E类网址判断 matchABCDE(ip, result); //私网IP判断 matchPrivateIp(ip, result); } System.out.println(result[0] + " " + result[1] + " " + result[2] + " " + result[3] + " " + result[4] + " " + result[5] + " " + result[6]); } private static void matchPrivateIp(String[] ip, int[] result) { if (Integer.parseInt(ip[0]) == 10) { result[6] = result[6] + 1; } if (Integer.parseInt(ip[0]) == 172 && (Integer.parseInt(ip[1]) >= 16 && Integer.parseInt(ip[1]) <= 31)) { result[6] = result[6] + 1; } if (Integer.parseInt(ip[0]) == 192 && Integer.parseInt(ip[1]) == 168) { result[6] = result[6] + 1; } } private static void matchABCDE(String[] ip, int[] result) { if (Integer.parseInt(ip[0]) >= 1 && Integer.parseInt(ip[0]) <= 126) { result[0] = result[0] + 1; } if (Integer.parseInt(ip[0]) >= 128 && Integer.parseInt(ip[0]) <= 191) { result[1] = result[1] + 1; } if (Integer.parseInt(ip[0]) >= 192 && Integer.parseInt(ip[0]) <= 223) { result[2] = result[2] + 1; } if (Integer.parseInt(ip[0]) >= 224 && Integer.parseInt(ip[0]) <= 239) { result[3] = result[3] + 1; } if (Integer.parseInt(ip[0]) >= 240 && Integer.parseInt(ip[0]) <= 255) { result[4] = result[4] + 1; } } private static boolean validateNetMask(String[] netmask, int[] result, int[] rightMask) { if (netmask.length != 4) { result[5] = result[5] + 1; return true; } int i0 = Integer.parseInt(netmask[0]); int i1 = Integer.parseInt(netmask[1]); int i2 = Integer.parseInt(netmask[2]); int i3 = Integer.parseInt(netmask[3]); //排除都是0 if (i0 == 0 && i1 == 0 && i2 == 0 && i3 == 0) { result[5] = result[5] + 1; return true; } //排除都是255 if (i0 == 255 && i1 == 255 && i2 == 255 && i3 == 255) { result[5] = result[5] + 1; return true; } if (Integer.parseInt(netmask[0]) == 255) { if (Integer.parseInt(netmask[1]) == 255) { if (Integer.parseInt(netmask[2]) == 255) { if (Integer.parseInt(netmask[3]) == 255) { } else { if (checkNetMask(netmask[3], rightMask)) { result[5] = result[5] + 1; return true; } } } else { if (checkNetMask(netmask[2], rightMask)) { result[5] = result[5] + 1; return true; } else { if (i3 == 0) { } else { result[5] = result[5] + 1; return true; } } } } else { if (checkNetMask(netmask[1], rightMask)) { result[5] = result[5] + 1; return true; } else { if (i2 == 0 && i3 == 0) { } else { result[5] = result[5] + 1; return true; } } } } else { if (checkNetMask(netmask[0], rightMask)) { result[5] = result[5] + 1; return true; } else { if (i1 == 0 && i2 == 0 && i3 == 0) { } else { result[5] = result[5] + 1; return true; } } } return false; } private static boolean checkNetMask(String s, int[] rightMask) { int count = 0; for (int i = 0; i < rightMask.length; i++) { if (rightMask[i] == Integer.parseInt(s)) { count = count + 1; } } if (count == 0) { return true; } return false; } private static boolean validateIp(String[] ip, int[] result) { if (ip.length != 4) { result[5] = result[5] + 1; return true; } else { int i0 = Integer.parseInt(ip[0]); int i1 = Integer.parseInt(ip[1]); int i2 = Integer.parseInt(ip[2]); int i3 = Integer.parseInt(ip[3]); if (i0 < 0 || i0 > 255 || i1 < 0 || i1 > 255 || i2 < 0 || i2 > 255 || i3 < 0 || i3 > 255) { result[5] = result[5] + 1; return true; } } return false; } private static boolean checkPrivate(String[] ip) { if (Integer.parseInt(ip[0]) == 0 || Integer.parseInt(ip[0]) == 127) { return true; } return false; } }
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
nextLine:
while (in.hasNextLine()) {
String s = in.nextLine();
String[] IP = s.split("~");
if (IP.length != 2) {
break nextLine;
}
// only 3 points
if (IP[0].matches(IPRegexLevel1) && IP[1].matches(IPRegexLevel1)) {
// IP
String[] ipNumberString = IP[0].split("\\.");
if (ipNumberString.length != 4) {
Wrong++;
continue nextLine;
}
int[] ipNumber = new int[4];
for (int i = 0; i < 4; i++) {
ipNumber[i] = Integer.parseInt(ipNumberString[i]);
if (ipNumber[i] < 0 || ipNumber[i] > 255) {
Wrong++;
continue nextLine;
}
}
if (ipNumber[0] == 0 || ipNumber[0] == 127) {
continue nextLine;
}
// Invalid Mask
String[] maskNumberString = IP[1].split("\\.");
if (maskNumberString.length != 4) {
Wrong++;
continue nextLine;
}
int[] maskNumber = new int[4];
for (int i = 0; i < 4; i++) {
maskNumber[i] = Integer.parseInt(maskNumberString[i]);
if (maskNumber[i] < 0 || maskNumber[i] > 255) {
Wrong++;
continue nextLine;
}
}
if (maskNumber[0] == 0 || maskNumber[3] == 255) {
Wrong++;
continue nextLine;
}
for (int i = 0; i < 3; i++) {
if(maskNumber[i] != 255) {
for (int j = i+1; j < 4; j++) {
if(maskNumber[j] != 0) {
Wrong++;
continue nextLine;
}
}
}
}
for (int i = 0; i < 4; i++) {
if(maskNumber[i] != 255) {
if (maskNumber[i] > 0 && maskNumber[i] < 255) {
String maskBinary = Integer.toBinaryString(maskNumber[i]);
if (maskBinary.length() < 8) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 8 - maskBinary.length(); j++) {
sb.append("0");
}
sb.append(maskBinary);
maskBinary = sb.toString();
// maskBinary = "0".repeat(8 - maskBinary.length()) + maskBinary;
}
boolean validMask = maskBinary.matches("^1{0,32}0{0,32}$");
if (!validMask) {
Wrong++;
continue nextLine;
}
}
}
}
// IP
// A
if (ipNumber[0]>0 && ipNumber[0] < 127) {
A++;
}
// B
if (ipNumber[0] > 127 && ipNumber[0] < 192) {
B++;
}
// C
if (ipNumber[0] >= 192 && ipNumber[0] <= 223){
C++;
}
// D
if (ipNumber[0] >= 224 && ipNumber[0] <= 239) {
D++;
}
// E
if (ipNumber[0] >= 240 && ipNumber[0] <= 255) {
E++;
}
// Private
if (ipNumber[0] == 10 || (ipNumber[0] == 172 && ipNumber[1] >= 16 && ipNumber[1] <= 31) || (ipNumber[0] == 192 && ipNumber[1] == 168)) {
Private++;
}
} else {
Wrong++;
continue nextLine;
}
}
System.out.println(A+" "+B+" "+C+" "+D+" "+E+" "+Wrong+" " + Private);
}
static int A=0,B=0,C=0,D=0,E=0,Wrong=0,Private=0;
public static final String IPRegexLevel1 = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$";
}
import java.util.Arrays;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
// 过滤掉0.* 127.*的地址
// 优先匹配私网ip
// 再匹配ABCDE类地址
// 子网掩码匹配,必须以1起始,并且全0或全1是非法的。
static int[] result = new int[7];
static long aLeft = 0x01000000L, aRight = 0x7effffffL;
static long bLeft = 0x80000000L, bRight = 0xbfffffffL;
static long cLeft = 0xc0000000L, cRight = 0xdfffffffL;
static long dLeft = 0xe0000000L, dRight = 0xefffffffL;
static long eLeft = 0xf0000000L, eRight = 0xffffffffL;
static long apl = 0x0a000000L, apr = 0x0affffffL;
static long bpl = 0xac100000L, bpr = 0xac31ffffL;
static long cpl = 0xc0a80000L, cpr = 0xc0a8ffffL;
public static long ip2Dec(String ip) {
String[] ipPoint = ip.split("\\.");
if (ipPoint.length != 4) return -1;
long sum = 0;
for (int i = 24, j = 0; j < 4; ++j, i -= 8) {
long a = Integer.parseInt(ipPoint[j]);
sum += a << i;
}
return sum;
}
public static boolean isIpValid(String ip) {
long ipDec = ip2Dec(ip);
return ipDec >= aLeft && ipDec <= eRight;
}
public static boolean isMarkValid(String mark) {
String[] ms = mark.split("\\.");
if (ms.length != 4 || ms[3].equals("255") || ms[0].equals("0")) return false;
String b = "";
for (String t : ms) {
b += String.format("%08d", Integer.parseInt(Integer.toBinaryString(Integer.parseInt(t))));
}
boolean findZero = false;
for (int i = 0; i < b.length(); ++ i) {
if (b.charAt(i) == '0') findZero = true;
if (findZero && b.charAt(i) == '1') return false;
}
return true;
}
public static boolean isPrivate(String ip) {
long a = ip2Dec(ip);
return (a >= apl && a <= apr) || (a >= bpl && a <= bpr) || (a >= cpl && a <= cpr);
}
public static void group(String ip) {
long a = ip2Dec(ip);
if (a >= aLeft && a <= aRight) result[0] ++;
if (a >= bLeft && a <= bRight) result[1] ++;
if (a >= cLeft && a <= cRight) result[2] ++;
if (a >= dLeft && a <= dRight) result[3] ++;
if (a >= eLeft && a <= eRight) result[4] ++;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String ipAndMark = in.nextLine();
String[] arr = ipAndMark.split("~");
if (arr[0].startsWith("0.") || arr[0].startsWith("127.")) continue;
if (!isIpValid(arr[0]) || !isMarkValid(arr[1])) {
result[5] ++;
}else {
if (isPrivate(arr[0])) {
result[6] ++;
}
group(arr[0]);
}
}
for (int r : result) System.out.print(r + " ");
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int A = 0, B = 0, C = 0, D = 0, E = 0, err = 0, pr = 0;
while (in.hasNext()) {
String val = in.nextLine();
//ip和子网掩码拆分
String[] vals = val .split("~");
//ip
String[] ips = vals[0].split("\\.");
//子网
String[] subnets = vals[1].split("\\.");
//校验无效值
if (ips[0].equals("0") || ips[0].equals("127")) {
continue;
}
//校验ip和子网掩码是否为空
if (!checkErr(ips) || !checkErr(subnets) ) {
err = err + 1;
continue;
}
//类型转换
//ip
Integer[] ipis = new Integer[4];
//子网
Integer[] subnetis = new Integer[4];
for (int i = 0 ; i < 4 ; i ++) {
ipis[i] = Integer.parseInt(ips[i]);
subnetis[i] = Integer.parseInt(subnets[i]);
if (ipis[i] > 255 || subnetis[i] > 255) {
System.out.println(val);
}
}
//校验错误的子网掩码
if (!checkSubnet(subnetis)) {
err = err + 1;
continue;
}
//校验ip类型
if (ipis[0] >= 1 && ipis[0] <= 126 ) {
A = A + 1;
} else if (ipis[0] >= 128 && ipis[0] <= 191 ) {
B = B + 1;
} else if (ipis[0] >= 192 && ipis[0] <= 223 ) {
C = C + 1;
} else if (ipis[0] >= 224 && ipis[0] <= 239 ) {
D = D + 1;
} else if (ipis[0] >= 240 && ipis[0] <= 255 ) {
E = E + 1;
}
//校验是否是私网ip
if (ipis[0] == 10) {
pr = pr + 1;
}
if (ipis[0].equals(172) && ipis[1] >= 16 && ipis[1] <= 31) {
pr = pr + 1;
}
if (ipis[0].equals(192) && ipis[1].equals(168)) {
pr = pr + 1;
}
}
System.out.print(A + " " + B + " " + C + " " + D + " " + E + " " + err + " " +
pr);
}
//校验是否存在空值
public static boolean checkErr(String[] val) {
for (String i : val) {
if (i == null || i.length() < 1) {
return false;
}
}
return true;
}
//校验子网掩码是否合法
public static boolean checkSubnet(Integer[] vals) {
if (vals[0].equals(0) || vals[3].equals(255)) {
return false;
}
//true 为1,false为0
Boolean flag = true;
for (Integer i : vals) {
StringBuilder val = new StringBuilder(Integer.toBinaryString(i));
if (val.length() < 8) {
for (int z = 0; z < 8 - val.length(); z++) {
val.insert(0,0);
}
}
int[] ss = val.chars().toArray();
for (Integer s : ss) {
if (flag && s.equals(48)) {
flag = false;
} else if (!flag && s.equals(49)) {
return false;
}
}
}
return true;
}
}