本题将会给出
条地址信息,确切数字未知,您需要一直读取至文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每条地址信息描述如下:
每行输入一个
形式的 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.*;
public class Main {
public static final long[] boundA = new long[] {
0x01000000L,
0x7EFFFFFFL
};
public static final long[] boundB = new long[] {
0x80000000L,
0xBFFFFFFFL
};
public static final long[] boundC = new long[] {
0xC0000000L,
0xDFFFFFFFL
};
public static final long[] boundD = new long[] {
0xE0000000L,
0xEFFFFFFFL
};
public static final long[] boundE = new long[] {
0xF0000000L,
0xFFFFFFFFL
};
public static final long[][] ignored = new long[][] {
new long[]{
0x00000000L,
0x00FFFFFFL
},
new long[] {
0x7F000000L,
0x7FFFFFFFL
}
};
public static final long[][] boundPrivates = new long[][] {
new long[] {
0x0A000000L,
0x0AFFFFFFL
},
new long[] {
0xAC0F0000L,
0xAC1F0000L
},
new long[] {
0xC0A80000L,
0xC0A8FFFFL
}
};
public static final long[] validMasks = new long[] {
(0xFFFFFFFFL << 1) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 2) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 3) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 4) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 5) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 6) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 7) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 8) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 9) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 10) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 11) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 12) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 13) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 14) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 15) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 16) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 17) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 18) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 19) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 20) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 21) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 22) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 23) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 24) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 25) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 26) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 27) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 28) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 29) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 30) & 0xFFFFFFFFL,
(0xFFFFFFFFL << 31) & 0xFFFFFFFFL
};
public static boolean rightMask(String maskstr) {
long mask = IP2Int(maskstr);
if(mask == -1) {
return false;
}
for(long i : validMasks) {
if( i == mask) {
return true;
}
}
return false;
}
public static boolean isPrivate(String ipstr) {
long ip = IP2Int(ipstr);
return (ip >= boundPrivates[0][0] && ip <= boundPrivates[0][1]) ||
(ip >= boundPrivates[1][0] && ip <= boundPrivates[1][1]) ||
(ip >= boundPrivates[2][0] && ip <= boundPrivates[2][1]) ;
}
public static int getIPType(long ip) {
if(ip >= boundA[0] && ip <= boundA[1]) {
return 1;
} else if(ip >= boundB[0] && ip <= boundB[1]) {
return 2;
} else if(ip >= boundC[0] && ip <= boundC[1]) {
return 3;
} else if(ip >= boundD[0] && ip <= boundD[1]) {
return 4;
} else if(ip >= boundE[0] && ip <= boundE[1]) {
return 5;
} else if((ip >= ignored[0][0] && ip <= ignored[0][1]) ||
(ip >= ignored[1][0] && ip <= ignored[1][1])) {
return -2;
}
else {
return 6;
}
}
public static long IP2Int(String str) {
long result = -1;
String[] split = str.split("\\.");
if(split.length != 4) {
return result;
}
result = 0;
for(String part : split) {
try {
result = (result << 8 | Integer.parseInt(part));
} catch (NumberFormatException ex) {
ex.printStackTrace();
result = -1;
break;
}
}
return result;
}
public static int identifyIP(String ipstr) {
int err = 0;
long ip = IP2Int(ipstr);
if(ip == -1) {
err = 6;
} else {
err = getIPType(ip);
}
return err;
}
public static void main(String[] args) {
int A = 0; //1
int B =0 ; //2
int C = 0; //3
int D = 0; //4
int E = 0; //5
int ERROR = 0; //6
int PRIVATE = 0; //7
Scanner sc = new Scanner(System.in);
while(true) {
String str;
if(sc.hasNextLine()) {
str = sc.nextLine();
} else {
break;
}
String[] split = str.split("~");
String ip = split[0];
String mask = split[1];
if(!rightMask(mask)) {
ERROR++;
} else {
int res = identifyIP(ip);
switch(res) {
case 1: A++;break;
case 2: B++;break;
case 3: C++;break;
case 4: D++;break;
case 5: E++;break;
case 6: ERROR++;break;
default: break;
}
if(res != 6) {
if(isPrivate(ip)) {
PRIVATE++;
}
}
}
}
System.out.printf("%d %d %d %d %d %d %d\n",A, B, C, D, E, ERROR, PRIVATE);
}
}
def checkyanma(yanma01str):
if yanma01str[0]=='0'&nbs***bsp;yanma01str[-1]=='1':
return False
ing = 1
for c in yanma01str:
if c=='1' and ing==0:
return False
if c=='0':
ing=0
return True
def checkip(iplist):
kind, si = 5, 0
for num in iplist:
if num<0&nbs***bsp;num>255:
return kind, si
if 1<=iplist[0]<=126: kind = 0
elif 128<=iplist[0]<=191: kind = 1
elif 192<=iplist[0]<=223: kind = 2
elif 224<=iplist[0]<=239: kind = 3
else: kind = 4
if iplist[0]==10: si = 1
elif iplist[0]==172 and 16<=iplist[1]<=31: si = 1
elif iplist[0]==192 and iplist[1]==168: si = 1
return kind, si
def solution(record):
ans = [0,0,0,0,0,0,0]
for _ in record:
ipstr, yanmastr = _[0], _[1]
iplist = list(map(int, [x for x in ipstr.split('.') if x]))
yanmalist = list(map(int, [x for x in yanmastr.split('.') if x]))
if len(iplist)!=4:
ans[5] += 1
continue
if len(yanmalist)!=4:
ans[5] += 1
continue
yanma01str = ''.join(list(map(lambda x:bin(x)[2:].rjust(8, '0'), yanmalist)))
if not checkyanma(yanma01str):
ans[5] += 1
continue
# 排除0和127开头
if iplist[0]==0&nbs***bsp;iplist[0]==127: continue
kind, si = checkip(iplist)
ans[kind]+=1
ans[-1]+=si
return ' '.join(list(map(str, ans)))
import sys
while True:
try:
record = []
#while True:
for line in sys.stdin:
# inputlist = sys.stdin.readline()
# inputlist = input()
# if inputlist=='':
# break
record.append(list(line.split('~')))
if record==[]:
break
else:
print(solution(record))
except:
break
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
//使用网络库函数判断 ip是否合规 -- 学到了
//1、使用fgets函数读入一行数据
//2、对每一行数据进行按照~切割成 ip 和子网掩码
//3、判断子网掩码(本题是 不能全1,且前面是连续的1后面是连续的0),则可以用子网掩码取反+1,则二进制只有一个1个1
//处理过的子网掩码再和处理过的子网掩码-1做与操作,若为0 则子网掩码ok
//4、判断ip的范围,来进行相应的变量计数即可
using namespace std;
int validmask(char * mask)
{
unsigned int n[4],res = 0;
int i ;
sscanf(mask,"%u.%u.%u.%u",&n[3],&n[2],&n[1],&n[0]);
if(n[3] == 255 && n[2] == 255 && n[1] == 255 && n[0]== 255)
return 0;
for(i = 0;i<4;i++)
{
res += n[i]<<(i*8);
}
res = ~res +1;
if((res & (res - 1)) == 0) return 1;
return 0;
}
int main()
{
char str[50];
int a = 0,b=0,c=0,d=0,e=0,err=0,pri=0;
while(fgets(str,50,stdin))
{
char *p = str;
char ip[2][20];
int i = 0;
int maskflag = 0;
while((p = strtok(p,"~") )!= NULL)
{
strcpy(ip[i],p);
i++;
p = NULL;
if(i == 2) i=0;
}
maskflag = validmask(ip[1]);
if(maskflag)
{
struct in_addr st;
unsigned int ip1,ip2;
int valid = inet_pton(AF_INET,ip[0],(void *)&st);
sscanf(ip[0],"%u.%u",&ip1,&ip2);
if(valid)
{
if(ip1>=1 && ip1 <=126)
a++;
else if(ip1>=128 && ip1 <=191)
b++;
else if(ip1>=192 && ip1 <=223)
c++;
else if(ip1>=224 && ip1 <=239)
d++;
else if(ip1>=240 && ip1 <=255)
e++;
if(ip1==10
|| (ip1==172 && ip2 >=16 &&ip2 <=31)
|| (ip1==192 && ip2 ==168))
pri ++;
}else
err++;
}else
{
err++;
}
}
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<err<<" "<<pri<<endl;
} import java.util.Scanner;
public class Main{
public static int pr;
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int A=0,B=0,C=0,E=0,D=0,error=0;
while(sc.hasNext()){
String s=sc.nextLine();
String sArr[]=s.split("~");
if(panduanyanma(sArr[1])){
char k=pandan***(sArr[0],sArr[1]);
if(k=='A'){A++;}else if(k=='B'){B++;}else if(k=='C'){C++;}
else if(k=='D'){D++;}else if(k=='E'){E++;}
else if(k=='n'){error++;}else{;}
}else{
error++;
}
}
System.out.println(A+" "+B+" "+C+" "+D+" "+E+" "+error+" "+pr);
}
public static boolean panduanyanma(String s){
String sArr[]=s.split("\\.");
int k[]=new int [sArr.length];
for(int i=0;i<sArr.length;i++){
//System.out.println(sArr[i].length()>3);
if(sArr[i].length()<=0||sArr[i].length()>3)
{ return false;}
k[i]=Integer.parseInt(sArr[i]);
//System.out.println(k[i]);
if(k[i]>255)return false;
if(i>0&&k[i-1]!=255&&k[i]!=0)return false;
if(i>0&&(k[i-1]==255)&&(k[i]!=0&&k[i]!=128&&k[i]!=192&&k[i]!=224&&k[i]!=240&&k[i]!=248&&k[i]!=252&&k[i]!=254&&k[i]!=255))
{ return false;}
if(i==sArr.length-1&&k[i]==255)return false;
}
return true;
}
public static char pandan***(String s,String d){
String sArr[]=s.split("\\.");
int k[]=new int [sArr.length];
String dArr[]=d.split("\\.");
int kd[]=new int [dArr.length];
for(int i=0;i<sArr.length;i++){
//System.out.println(sArr[i].length()>3);
if(sArr[i].length()<=0||sArr[i].length()>3)
{ return 'n';}
k[i]=Integer.parseInt(sArr[i]);
kd[i]=Integer.parseInt(dArr[i]);
if(k[i]>255)return 'n';
k[i]=k[i]&kd[i];
}
if(k[0]==10&&k[1]>=0&&k[2]>=0&&k[3]>=0&&k[1]<=255&&k[2]<=255&&k[3]<=255){
pr++;
}
if(k[0]==172&&k[1]>=16&&k[2]>=0&&k[3]>=0&&k[1]<=31&&k[2]<=255&&k[3]<=255){
pr++;
}
if(k[0]==192&&k[1]==168&&k[2]>=0&&k[3]>=0&&k[2]<=255&&k[3]<=255){
pr++;
}
if(k[0]>=1&&k[0]<=126&&k[1]>=0&&k[2]>=0&&k[3]>=0&&k[1]<=255&&k[2]<=255&&k[3]<=255){
return 'A';
}
if(k[0]>=128&&k[0]<=191&&k[1]>=0&&k[2]>=0&&k[3]>=0&&k[1]<=255&&k[2]<=255&&k[3]<=255){
return 'B';
}
if(k[0]>=192&&k[0]<=223&&k[1]>=0&&k[2]>=0&&k[3]>=0&&k[1]<=255&&k[2]<=255&&k[3]<=255){
return 'C';
}
if(k[0]>=224&&k[0]<=239&&k[1]>=0&&k[2]>=0&&k[3]>=0&&k[1]<=255&&k[2]<=255&&k[3]<=255){
return 'D';
}
if(k[0]>=240&&k[0]<=255&&k[1]>=0&&k[2]>=0&&k[3]>=0&&k[1]<=255&&k[2]<=255&&k[3]<=255){
return 'E';
}
return 'o';
}
} 使用两个方法,一个是判断子网掩码是否符合要求,其符合才会去考虑ip地址。如果子网掩码不符合,那么ip地址就也算错误,不做考虑。另一个方法是判断地址是否符合,这里面直接做与,然后判断每一个数字对应的是哪个地址段。
c语言版
#include <stdio.h>
#include <string.h>
int main(){
char s[20];
int i,j,len,a=0,b=0,c=0,d=0,e=0,err=0,pri=0,flag=0,num=0,p=0,ip[8];
bool error,fg;
while(scanf("%s",s)!=EOF){
p=0;flag=0;error=false;fg=false;//p指向ip[p],flag标记数字字符起始位置,error判断输入是否有错误,fg子网掩码是否正确
len=strlen(s);
for(i=0;i<len+1;i++){ //提起输入字符串里的数字并且判断输入格式是否正确
if(s[i]>='0'&&s[i]<='9')
continue;
else if(s[i]=='.'||s[i]=='~'||s[i]=='\0'){
num=0;
for(j=flag;j<i;j++)
num=num*10+(s[j]-'0');
ip[p++]=num;
flag=i+1;
if(num<0||num>255){
err++;
error=true; //格式错误
break;
}
if(s[i]!='\0')
if(s[flag]<'0'||s[flag]>'9'){
err++;
error=true;
break;
}
}
else{
err++;
error=true;
break;
}
}
if(!error){
num=0;
if(ip[4]==0||ip[7]==255) //判断子网掩码是否正确
err++;
else{
for(i=4;i<=7;i++){
if(ip[i]==255)
continue;
for(j=i+1;j<=7;j++)
num+=ip[j];
if((ip[i]==254||ip[i]==252||ip[i]==248||ip[i]==240||ip[i]==224||ip[i]==192||ip[i]==128||ip[i]==0)&&num==0)
fg=true; //子网掩码正确
else
err++;
break;
}
}
if(fg){ //子网掩码正确的情况下判断是哪一类ip
if(ip[0]>=1&&ip[0]<=126) a++;
else if(ip[0]>=128&&ip[0]<=191) b++;
else if(ip[0]>=192&&ip[0]<=223) c++;
else if(ip[0]>=224&&ip[0]<=239) d++;
else if(ip[0]>=240&&ip[0]<=255) e++;
if(ip[0]==10||(ip[0]==172&&ip[1]>=16&&ip[1]<=31)||(ip[0]==192&&ip[1]==168))
pri++;
}
}
}
printf("%d %d %d %d %d %d %d\n",a,b,c,d,e,err,pri);
return 0;
} #include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
unsigned int A[2] = {0x01000000,0x7EFFFFFF}; //A类网地址范围
unsigned int B[2] = {0x80000000,0xBFFFFFFF}; //B类网地址范围
unsigned int C[2] = {0xC0000000,0xDFFFFFFF}; //C类网地址范围
unsigned int D[2] = {0xE0000000,0xEFFFFFFF}; //D类网地址范围
unsigned int E[2] = {0xF0000000,0xFFFFFFFF}; //E类网地址范围
unsigned int F[2] = {0x0A000000,0x0AFFFFFF}; //私网地址范围1
unsigned int G[2] = {0xAC100000,0xAC1FFFFF}; //私网地址范围2
unsigned int H[2] = {0xC0A80000,0xC0A8FFFF}; //私网地址范围3
//将地址/掩码转换为unsigned int
unsigned int string2int(string str)
{
int io = 0;
unsigned int mas = 0;
while((int)(str.find('.',io))>=0)
{
int i = str.find('.',io);
if(i-io==0)
return 0;
istringstream ss;
ss.str(str.substr(io,i-io));
io = i+1;
unsigned int temp;
ss>>temp;
mas<<=8;
mas|=temp;
}
istringstream ss;
ss.str(str.substr(io));
unsigned int temp;
ss>>temp;
mas<<=8;
mas|=temp;
return mas;
}
//检查掩码是否合法
bool checkmask(unsigned int mas)
{
//255.255.255.255的掩码的非法的
if(mas==0xFFFFFFFF)
return false;
unsigned int flag = 1;
while(flag!=0)
{
if((mas&flag)==0)
{
flag<<=1;
continue;
}
else
{
while(flag!=0)
{
if((mas&flag)==0)
return false;
flag<<=1;
}
}
}
return true;
}
//寻找IP的类别
int checkipad(unsigned int ipa)
{
if(ipa>=A[0]&&ipa<=A[1])
{
if(ipa>=F[0]&&ipa<=F[1])
return 11;
return 1;
}
else if(ipa>=B[0]&&ipa<=B[1])
{
if(ipa>=G[0]&&ipa<=G[1])
return 12;
return 2;
}
else if(ipa>=C[0]&&ipa<=C[1])
{
if(ipa>=H[0]&&ipa<=H[1])
return 13;
return 3;
}
else if(ipa>=D[0]&&ipa<=D[1])
return 4;
else if(ipa>=E[0]&&ipa<=E[1])
return 5;
return 0;
}
//主函数
int main()
{
string str;
vector<string> ipad;
vector<string> mask;
vector<int> res(7,0);
while(getline(cin,str))
{
int pos = str.find('~');
ipad.push_back(str.substr(0,pos));
mask.push_back(str.substr(pos+1));
}
{
unsigned int i;
vector<int> ip;
vector<int> im;
for(i=0;i<ipad.size();++i)
{
unsigned int tep = string2int(ipad[i]);
unsigned int tem = string2int(mask[i]);
if(tep==0||tem==0)
{
res[5] += 1;
continue;
}
else
{
int bop = checkipad(tep);
bool bom = checkmask(tem);
if(!bom)
{
res[5] += 1;
continue;
}
else
{
switch(bop)
{
case 1:
res[0] += 1;
break;
case 2:
res[1] += 1;
break;
case 3:
res[2] += 1;
break;
case 4:
res[3] += 1;
break;
case 5:
res[4] += 1;
break;
case 11:
res[0] += 1;
res[6] += 1;
break;
case 12:
res[1] += 1;
res[6] += 1;
break;
case 13:
res[2] += 1;
res[6] += 1;
break;
}
}
}
}
}
unsigned int i;
for(i=0;i<res.size();++i)
{
cout<<res[i];
if(i<res.size()-1)
cout<<' ';
}
cout<<endl;
return 0;
}
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
vector<string> split(string str,char del)
{
stringstream ss(str);
string tok;
vector<string> ret;
while(getline(ss,tok,del))
{
if(tok > "")
ret.push_back(tok);
}
return ret;
}
int main()
{
string all;
int result[7]={0};
while(getline(cin,all))
{
vector<string> ipyanma;
ipyanma = split(all,'~');
vector<string> ip,yanma;
ip = split(ipyanma[0],'.');
yanma = split(ipyanma[1],'.');
unsigned int yyy=0,yuan=0,flag=0;
if(ip.size() == 4 && yanma.size() == 4)
{
for(int kk=0;kk<4;kk++)
{
unsigned int temp11= atoi(yanma[kk].c_str());
yyy += temp11 << ((3-kk)*8);
}
yuan = yyy;
yyy = (~yyy) +1;
flag = yyy & (yyy-1);
if((flag ==0) && (yuan != 0xffffffff) && (yuan != 0) )
{
if(atoi(ip[0].c_str()) >= 1 && atoi(ip[0].c_str()) <= 126)
result[0] += 1;
if(atoi(ip[0].c_str())>=128 && atoi(ip[0].c_str())<=191)
result[1] += 1;
if(atoi(ip[0].c_str())>=192 && atoi(ip[0].c_str())<=223)
result[2] += 1;
if(atoi(ip[0].c_str())>=224 && atoi(ip[0].c_str())<=239)
result[3] += 1;
if(atoi(ip[0].c_str())>=240 && atoi(ip[0].c_str())<=255)
result[4] += 1;
if((atoi(ip[0].c_str())>=10 && atoi(ip[0].c_str())<=10) ||
(atoi(ip[0].c_str())>=172 && atoi(ip[1].c_str())>=16 &&
atoi(ip[0].c_str())<=172 && atoi(ip[1].c_str())<=31) ||
(atoi(ip[0].c_str())>=192 && atoi(ip[1].c_str())>=168 &&
atoi(ip[0].c_str())<=192 && atoi(ip[1].c_str())<=168))
result[6] += 1;
}
else
{
result[5] += 1;
}
}
else
{
result[5] += 1;
}
}
cout<<result[0]<<" "<<result[1]<<" "<<result[2]<<" "<<result[3]<<" "<<result[4]<<" "<<result[5]<<" "<<result[6]<<endl;
return 0;
}
//完美AC,
思路:
首先必须注意当ip属于某类地址同时属于私有地址都需要进行加1
1)先判断子网掩码是否合法,如果不合法则直接error+=1
思路:说下,我的判断子网掩码的方法与上述不同,既然子网掩码为前面的连续的1,后面为连续的0,
我们可以计算前面2个01串不同的次数,如果为2则说明是正确的,如果超过2,说明不对。这里也正好
不需要判断子网255.255.255.255和0.0.0.0等特殊的子网掩码,在我的方法里这2种特殊子网掩码是不合法的。
2)判断属于哪类地址
3)判断是否是私有地址
import java.util.*;
public class Main_01{
public static void main(String[]args){
int typeA=0;
int typeB=0;
int typeC=0;
int typeD=0;
int typeE=0;
int error=0;
int privateIp=0;
Scanner s=new Scanner(System.in);
while(s.hasNext()){
String information=s.next();
String [] info=information.split("~");
if(info==null||info.length!=2)
break;
String ip=info[0];
String mask=info[1];
String []ips=ip.split("\\.");
if(ips==null||ips.length<4)
{
error+=1;
// System.out.println("out..");
continue;
}
int v=Integer.parseInt(ips[0]);
int two=Integer.parseInt(ips[1]);
//process mask
boolean isMask=checkMask(mask);
if(!isMask){
error+=1;
continue;//不进行下面处理
}
//处理ip地址
if(v>=1&&v<=126)
typeA++;
else if(v>=128&&v<=191)
typeB+=1;
else if(v>=192&&v<=223)
typeC+=1;
else if(v>=224&&v<=239)
typeD+=1;
else if(v>=240&&v<=255)
typeE+=1;
if(v==10||(v==172&&two>=16&&two<=31)||(v==192&&two==168))
privateIp+=1;
} //end while
System.out.println(typeA+" "+typeB+" "+typeC+" "+typeD+" "+typeE+" "+error+" "+privateIp);
}
public static boolean checkMask(String mask){
String []masks=mask.split("\\.");
int v=-1;
int changeTime=0;//计算相邻数不同的次数
for(int i=masks.length-1;i>=0;i--){
int times=0;
int value=Integer.parseInt(masks[i]);
while(times<8){
if((value&1)!=v)
{
changeTime+=1;
v=value&1;
}
if(changeTime>=3)
return false;
value>>=1;
times++;
} //end while
} //end for
return changeTime==2?true:false;
}
}
遇到的一些问题:
1.掩码不能是全一,也不能是全零。
2.IP以0或127开头不算错误,只是不用来统计。
import java.util.*;
public class Main {
/*
*子网掩码不为255或者0时,只能是这些情况
*/
private static Set<Integer> set = new HashSet<>(Arrays.asList(128, 192, 224, 240, 248, 252, 254, 255));
/*
*将掩码变成数组,并检查是否有错,如果有错返回null
*/
private static int[] changeMask(String[] s) {
if (s == null || s.length != 4) {
return null;
}
int[] a = new int[4];
for (int i = 0; i < 4; ++i) {
if (s[i] == null || s[i].length() == 0) {
return null;
}
int n = Integer.parseInt(s[i]);
if (n < 0 || n > 255) {
return null;
}
a[i] = n;
}
if (a[0] == 0 || a[3] == 255) {//掩码不能全为1,也不能全是0
return null;
}
int i = 3;
for (; i >= 0 && a[i] == 0; --i)
;
if (!set.contains(a[i])) {
return null;
}
--i;
for (; i >= 0 && a[i] == 255; --i)
;
if (i != -1) {
return null;
}
return a;
}
/*
* 将IP变成数组,并检查是否有错,如果有错返回null
*/
private static int[] changeIp(String[] s) {
if (s == null || s.length != 4) {
return null;
}
int[] a = new int[4];
for (int i = 0; i < 4; ++i) {
if (s[i] == null || s[i].length() == 0) {
return null;
}
int n = Integer.parseInt(s[i]);
if (n < 0 || n > 255) {
return null;
}
a[i] = n;
}
return a;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a = 0, b = 0, c = 0, d = 0, e = 0, wrong = 0, pri = 0;
while (cin.hasNext()) {
String[] s = cin.next().split("~");//将字符串切分为ip和掩码字符串
String[] ipStr = s[0].split("\\."),
maskStr = s[1].split("\\.");//将码串按.切分为字符串数组
int[] ip = changeIp(ipStr);
int[] mask = changeMask(maskStr);//将码串数组转换为数字数组,并检查两个串的合法性
if (ip == null || mask == null) {//如果任意一个串不合法,++wrong
++wrong;
continue;
}
int first = ip[0];
int second = ip[1];//主要判断前两个数
if (first >= 1 && first <= 126) {
++a;
if (first == 10) {
++pri;
}
} else if (first >= 128 && first <= 191) {
++b;
if (first == 172) {
if (second >= 16 && second <= 31) {
++pri;
}
}
} else if (first >= 192 && first <= 223) {
++c;
if (first == 192 && second == 168) {
++pri;
}
} else if (first >= 224 && first <= 239) {
++d;
} else if (first >= 240 && first <= 255) {
++e;
}
}
System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + wrong + " " + pri);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] result = new int[7];
while (in.hasNext()) {
String[] inputArr = in.nextLine().split("~", -1);
if (inputArr.length != 2) continue;
String ip = inputArr[0];
String subnetMask = inputArr[1];
// 错误IP地址或错误掩码
if (!isValidIP(ip, subnetMask)) {
result[5]++;
continue;
}
if (ip.matches("^10(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")
|| ip.matches("^172\\.(1[6-9]|2\\d|3[01])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){2}$")
|| ip.matches("^192\\.168(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){2}$")) {
// 私有IP check
result[6]++;
}
if (ip.matches("^([1-9]|[1-9]\\d|1[01]\\d|12[0-6])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")) {
// A类IP
result[0]++;
} else if (ip.matches("^(12[89]|1[3-8]\\d|19[01])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")) {
// B类IP
result[1]++;
} else if (ip.matches("^(19[2-9]|2[01]\\d|22[0-3])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")) {
// C类IP
result[2]++;
} else if (ip.matches("^(22[4-9]|23\\d)(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")) {
// D类IP
result[3]++;
} else if (ip.matches("^(24\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")) {
// E类IP
result[4]++;
}
}
System.out.println(result[0] + " " + result[1] + " " + result[2] + " "
+ result[3] + " " + result[4] + " " + result[5] + " " + result[6]);
in.close();
}
private static boolean isValidIP(String ip, String subnetMask) {
boolean isValid = false;
if (ip.matches("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")
&& subnetMask.matches("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$")) {
String[] arr = subnetMask.split("\\.");
String tmp = padLeft(Integer.toBinaryString(Integer.parseInt(arr[0], 10)))
+ padLeft(Integer.toBinaryString(Integer.parseInt(arr[1], 10)))
+ padLeft(Integer.toBinaryString(Integer.parseInt(arr[2], 10)))
+ padLeft(Integer.toBinaryString(Integer.parseInt(arr[3], 10)));
if (tmp.matches("^[1]+[0]+$"))
isValid = true;
}
return isValid;
}
private static String padLeft(String str) {
str = "00000000" + str;
return str.substring(str.length() - 8);
}
}
package cn.zhangxf0406.chinamobile;
import java.util.*;
import java.util.regex.*;
/**
*
* @author zhangxf0406
* 识别有效的IP地址和掩码并进行分类统计
*/
public class Demo1120 {
public static boolean judgeIP(String sIP){
Pattern pattern = Pattern.compile("^(\\d{1,})\\.(\\d{1,})\\.(\\d{1,})\\.(\\d{1,})$");
Matcher match = pattern.matcher(sIP);
if(match.matches()){
String[] s1 = sIP.split("\\.");
for(String ss : s1){
int n = Integer.parseInt(ss);
if(n < 0 || n > 255)
return false;
}
return true;
}else
return false;
}
//掩码的规则:前面为1 ,后面为0
public static boolean judgeMask(String sMask){
String [] s1 = sMask.split("\\.");
StringBuilder sb = new StringBuilder();
for(String ss :s1){
String temp = Integer.toBinaryString(Integer.parseInt(ss));
// int len = temp.length();
while(temp.length() != 8){
temp = "0"+temp;
}
sb.append(temp);
}
String binaryString = sb.toString();
int index0 = binaryString.indexOf("0");
//不能以0开始
if(index0 == 0)
return false;
//不能全是1
if(binaryString.matches("^1{1,}"))
return false;
//前面为1 ,后面为0
String[] s2 = binaryString.split("0");
if(s2.length == 1)
return true;
return false;
}
/*
* 私网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
*/
public static boolean judgePrivateIP(String ip){
String[] s1 = ip.split("\\.");
int a = Integer.parseInt(s1[0]);
int b = Integer.parseInt(s1[1]);
int c = Integer.parseInt(s1[2]);
int d = Integer.parseInt(s1[3]);
if(a == 10)
return true;
if(a == 172){
if(b >=16 && b <= 31)
return true;
}
if(a == 192 && b == 168)
return true;
return false;
}
public static void main(String[] args) {
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;
int errorIpOrMask = 0;
int privateIp = 0;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.next();
String[] s1 = str.split("~");
String ip = s1[0];
String mask = s1[1];
if(!judgeIP(ip) || !judgeMask(mask)){
errorIpOrMask++;
// System.out.println(A+" "+B+" "+C+" "+D+" "+E+" "+errorIpOrMask+" "+privateIp);
// System.out.println(errorIpOrMask);
continue;
}
//判断A,B,C,D,E
String firstStr = ip.substring(0,ip.indexOf("."));
int num = Integer.parseInt(firstStr);
if(num >= 1 && num <= 126){
A++;
}else if(num >= 128 && num <= 191){
B++;
}else if(num >= 192 && num <= 223){
C++;
}else if(num >= 224 && num <= 239){
D++;
}else if(num >= 240 && num <= 255)
E++;
//判断私有
if(judgePrivateIP(ip))
privateIp++;
System.out.println(A+" "+B+" "+C+" "+D+" "+E+" "+errorIpOrMask+" "+privateIp);
}
// System.out.println(A+" "+B+" "+C+" "+D+" "+E+" "+errorIpOrMask+" "+privateIp);
scanner.close();
}
}
import java.util.*;
public class Main {
private static final long[] PRIVATE_IP_RANGE = new long[6];
static {
PRIVATE_IP_RANGE[0] = str2ip("10.0.0.0");
PRIVATE_IP_RANGE[1] = str2ip("10.255.255.255");
PRIVATE_IP_RANGE[2] = str2ip("172.16.0.0");
PRIVATE_IP_RANGE[3] = str2ip("172.31.255.255");
PRIVATE_IP_RANGE[4] = str2ip("192.168.0.0");
PRIVATE_IP_RANGE[5] = str2ip("192.168.255.255");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int typeA, typeB, typeC, typeD, typeE, errorIPorMask, privateIP;
typeA = typeB = typeC = typeD = typeE = errorIPorMask = privateIP = 0;
while (sc.hasNext()) {
String[] address = sc.next().split("~");
if (!isLegalFormat(address[0]) || !isLegalFormat(address[1])) {
errorIPorMask++;
continue;
}
long ip = str2ip(address[0]);
long mask = str2ip(address[1]);
if (!isLegalMask(mask)) {
errorIPorMask++;
continue;
}
if (isPrivateIP(ip)) privateIP++;
int head = (int) (ip >> 24);
if (head >= 1 && head <= 126) typeA++;
else if (head >= 128 && head <= 191) typeB++;
else if (head >= 192 && head <= 223) typeC++;
else if (head >= 224 && head <= 239) typeD++;
else if (head >= 240 && head <= 255) typeE++;
}
System.out.printf("%d %d %d %d %d %d %d%n", typeA, typeB, typeC, typeD, typeE, errorIPorMask, privateIP);
}
private static long str2ip(String address) {
long ip = 0;
String[] nums = address.split("\\.");
for (String n : nums) {
ip <<= 8;
ip += (long)Integer.parseInt(n);
}
return ip;
}
private static boolean isLegalFormat(String address) {
return address.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$");
}
private static boolean isPrivateIP(long ip) {
return (ip >= PRIVATE_IP_RANGE[0] && ip <= PRIVATE_IP_RANGE[1])
|| (ip >= PRIVATE_IP_RANGE[2] && ip <= PRIVATE_IP_RANGE[3])
|| (ip >= PRIVATE_IP_RANGE[4] && ip <= PRIVATE_IP_RANGE[5]);
}
private static boolean isLegalMask(long mask) {
if (mask == 0 || mask == (1L<<32)-1) return false;
while ((mask & 1) == 0) mask >>= 1;
return (mask & (mask + 1)) == 0;
}
}
// 几点注意
// 1. mask 255.255.255.255 和 0.0.0.0 也是无效的
// 2. 私有ip和A~E类IP是不冲突的,统计的时候都要+1
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned int uint;
// A-E类地址的上下界
uint class_a_ip_low = (((1 << 8) << 8) << 8);
uint class_a_ip_high = (((((126 << 8) | 255) << 8) | 255) << 8) | 255;
uint class_b_ip_low = (((128 << 8) << 8) << 8);
uint class_b_ip_high = (((((191 << 8) | 255) << 8) | 255) << 8) | 255;
uint class_c_ip_low = (((192 << 8) << 8) << 8);
uint class_c_ip_high = (((((223 << 8) | 255) << 8) | 255) << 8) | 255;
uint class_d_ip_low = (((224 << 8) << 8) << 8);
uint class_d_ip_high = (((((239 << 8) | 255) << 8) | 255) << 8) | 255;
uint class_e_ip_low = (((240 << 8) << 8) << 8);
uint class_e_ip_high = (((((255 << 8) | 255) << 8) | 255) << 8) | 255;
// 3类私有地址的上下界
uint private_ip1_low = (((10 << 8) << 8) << 8);
uint private_ip1_high = (((((10 << 8) | 255) << 8) | 255) << 8) | 255;
uint private_ip2_low = ((((172 << 8) | 16) << 8) << 8);
uint private_ip2_high = (((((172 << 8) | 31) << 8) | 255) << 8) | 255;
uint private_ip3_low = ((((192 << 8) | 168) << 8) << 8);
uint private_ip3_high = (((((192 << 8) | 168) << 8) | 255) << 8) | 255;
// 根据给定符号分割字符串
vector<string> split(const string &ip, char ch)
{
vector<string> result;
int start = 0;
for (int i = 0; i < ip.size(); ++i)
{
if (ip[i] == ch)
{
result.push_back(string(ip.begin() + start, ip.begin() + i));
start = i + 1;
}
}
if (start < ip.size())
{
result.push_back(string(ip.begin() + start, ip.end()));
}
return result;
}
// str 转 ip
bool str2ip(vector<string> &ips, uint &ip, bool check_mask = false)
{
if (ips.size() != 4)
return false;
ip = 0;
for (int i = 0; i < ips.size(); ++i)
{
ip <<= 8;
int tmp = atoi(ips[i].c_str());
if (tmp < 0 || tmp > 255)
return false;
ip |= tmp;
}
// 如果是mask还要做检查
if (check_mask)
{
if (ip == 0xffffffff || ip == 0) // !255.255.255.255 和 0.0.0.0 也是无效的
return false;
uint tmp = ip;
uint test = 1 << 31;
while (tmp > 0)
{
if ((tmp & test) == 0)
return false;
tmp <<= 1;
}
}
return true;
}
int main(void)
{
string input;
int cnts[7] = { 0 };
uint class_low[] = { private_ip1_low, private_ip2_low, private_ip3_low, class_a_ip_low,
class_b_ip_low, class_c_ip_low, class_d_ip_low, class_e_ip_low };
uint class_high[] = { private_ip1_high, private_ip2_high, private_ip3_high, class_a_ip_high,
class_b_ip_high, class_c_ip_high, class_d_ip_high, class_e_ip_high };
while (cin >> input)
{
vector<string> ip_mask = split(input, '~');
vector<string> ip_strs = split(ip_mask[0], '.');
vector<string> mask_strs = split(ip_mask[1], '.');
uint ip, mask;
// 检查mask是否有效
if (str2ip(mask_strs, mask, true) == false)
{
cnts[5]++;
continue;
}
// 检查ip是否有效
if (str2ip(ip_strs, ip) == false)
{
cnts[5]++;
continue;
}
// 统计
for (int i = 0; i < 8; ++i)
{
if (ip >= class_low[i] && ip <= class_high[i])
{
if (i <= 2)
cnts[6]++;
else
{
cnts[i - 3] ++;
break;
}
}
}
}
cout << cnts[0] << " " << cnts[1] << " " << cnts[2] << " " << cnts[3] << " "
<< cnts[4] << " " << cnts[5] << " " << cnts[6] << endl;
return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
unsigned long long range[8][2] = {
{10000000000,10255255255},
{172016000000,17231255255},
{192168000000,192168255255},
{1000000000,126255255255},
{128000000000,191255255255},
{192000000000,223255255255},
{224000000000,239255255255},
{240000000000,255255255255},
};
int explain(char* ip){
int expect = 1;
int cnt = 0;
for(int i = 0 ; i < strlen(ip) ; i ++){
if(ip[i] >= '0' && ip[i] <= '9'){
expect = 1;
}
else{
if(expect == 0) return -1;
expect = 0;
cnt ++;
}
}
if(cnt != 3) return -1;
return 1;
}
int main(){
char ip[20] , code[20];
int a = 0 , b = 0 , c = 0 , d = 0 , e = 0 ,eip = 0 , sip = 0;
int cnt = 0;
while(scanf("%[^~]~%s",ip,code) != EOF){
int cds = explain(code);
int flag = 0;
if(cds > 0){
int aa[4];
sscanf(code , "%d.%d.%d.%d" , &aa[0],&aa[1],&aa[2],&aa[3]);
if(aa[0] > 255 || aa[1] > 255 || aa[2] > 255 || aa[3] > 255 || (aa[0] == 255 && aa[1] == 255 && aa[2] ==255 &&aa[3]==255 )){
eip ++;
flag = 1;
}
int exp = 1;
for(int i = 0 ; !flag && i < 4 ; i ++){
if(exp == 0 && aa[i] || aa[i] > 255){
eip ++;
flag = 1;
break;
}
int a1 = aa[i];
if(a1 < 255){
if(a1 == 128 || a1 == 192 || a1 == 224 || a1 == 240 || a1 == 248 || a1 == 252 || a1 == 254){
exp = 0;
}else if(a1 == 0 && i > 0){
exp = 0;
}
else{
eip ++;
flag = 1;
break;
}
}
}
}else{
flag = 1;
eip ++;
}
if(flag == 0){
int ips = explain(ip);
if(ips > 0){
int a1 , b1 ,c1 ,d1;
sscanf(ip , "%d.%d.%d.%d" , &a1,&b1,&c1,&d1);
if(a1>255 || b1 > 255 || c1 > 255 || d1 > 255){
eip++;
flag = 1;
}
if(flag == 0){
long long ans = a1*1e9 + b1*1e6 + c1*1e3 + d1;
//cout<<ans<<endl;
int i = 0;
for( ; i < 8 ; i ++){
if(ans <= range[i][1] && ans >= range[i][0]){
// cout<<i<<endl;
if(i == 3) a ++;
if(i == 4) b ++;
if(i == 5) c ++;
if(i == 6) d ++;
if(i == 7) e ++;
if(i == 0 || i == 1 || i == 2) sip ++;
//break;
}
}
}
}else{
eip ++;
}
}
memset(ip,'\0',sizeof ip);
memset(code,'\0',sizeof(code));
getchar();
}
printf("%d %d %d %d %d %d %d\n",a,b,c,d,e,eip,sip);
return 0;
}
def islegalIP(ip):
temp = ip.split('.')
for t in temp:
if t and int(t)>=0 and int(t)<=255:
continue
else:
return False
return True
def islegalMask(mask):
temp = mask.split('.')
if not islegalIP(mask):
return False
mask_str=''
for t in temp:
tt=bin(int(t)).split('b')[1]
while len(tt)<8:
tt='0'+tt
mask_str+=tt
len1=mask_str.count('1')
len0=mask_str.count('0')
if len1==len(mask_str)&nbs***bsp;len0==len(mask_str):
return False
if '1'*len1+'0'*len0 == mask_str:
return True
else:
return False
def isomit(ip):
temp = ip.split('.')[0]
if int(temp[0])==0&nbs***bsp;int(temp[0])==127:
return True
else:
return False
def trans2dec(ip):
temp = ip.split('.')
ip_str=''
for t in temp:
tt=bin(int(t)).split('b')[1]
while len(tt)<8:
tt='0'+tt
ip_str+=tt
return int(ip_str,base=2)
ip_list=[]
while True:
try:
ip_list.append(raw_input().strip().split('~'))
except:
break
A,B,C,D,E,I,P=0,0,0,0,0,0,0
for ip,mask in ip_list:
if not islegalIP(ip)&nbs***bsp;not islegalMask(mask):
I+=1
continue
if islegalIP(ip) and islegalMask(mask) and isomit(ip):
continue
if trans2dec(ip)>=trans2dec('1.0.0.0') and trans2dec(ip)<=trans2dec('126.255.255.255'):
A+=1
elif trans2dec(ip)>=trans2dec('128.0.0.0') and trans2dec(ip)<=trans2dec('191.255.255.255'):
B+=1
elif trans2dec(ip)>=trans2dec('192.0.0.0') and trans2dec(ip)<=trans2dec('223.255.255.255'):
C+=1
elif trans2dec(ip)>=trans2dec('224.0.0.0') and trans2dec(ip)<=trans2dec('239.255.255.255'):
D+=1
elif trans2dec(ip)>=trans2dec('240.0.0.0') and trans2dec(ip)<=trans2dec('255.255.255.255'):
E+=1
if (trans2dec(ip)>=trans2dec('10.0.0.0') and trans2dec(ip)<=trans2dec('10.255.255.255'))&nbs***bsp; (trans2dec(ip)>=trans2dec('172.16.0.0') and trans2dec(ip)<=trans2dec('172.31.255.255'))&nbs***bsp; (trans2dec(ip)>=trans2dec('192.168.0.0') and trans2dec(ip)<=trans2dec('192.168.255.255')):
P+=1
print A,B,C,D,E,I,P import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int IPA=0,IPB=0,IPC=0,IPD=0,IPE=0;
int errorIP=0,priIP=0;
String str="";
int n=4;
while(n--!=0){
str=in.next();
String[] split = str.split("~");
String pre=split[0];
String nex=split[1];
String[] preArr = pre.split("\\.");
String[] nexArr = nex.split("\\.");
int preLen=preArr.length;
int nexLen=nexArr.length;
//nexArr
boolean hasErr=false;
StringBuffer sb=new StringBuffer();
for (int j = 0; j < nexLen; j++) {
String ts=nexArr[j];
if(ts.equals("")||ts==null){
errorIP++;
// System.out.println("掩码错误");
hasErr=true;
break;
}
sb.append(Integer.toBinaryString(Integer.valueOf(ts)));
}
if(hasErr){
continue;
}
if(sb.toString().equals("255255255255")||sb.toString().equals("0000")){
errorIP++;
// System.out.println("掩码错误");
continue;
}
char[] sbArr = sb.toString().toCharArray();
int sbArrlen=sbArr.length;
int isOne=0,isZero=0;
boolean flag=false;
for (int i = 0; i < sbArrlen; i++) {
char c = sbArr[i];
if(c=='1'){
if(!flag)
isOne++;
}
else{
flag=true;
isZero++;
}
}
int sumLen=isOne+isZero;
if(sumLen!=sbArrlen){
errorIP++;
continue;
// System.out.println("掩码错误");
}
//preArr
for(int i=0;i<preLen;i++){
String ts=preArr[i];
if(ts.equals("")||ts==null){
errorIP++;
break;
}
int num=Integer.valueOf(ts);
if(num>=0&&num<=255){
if(i==preLen-1){
int tn=Integer.valueOf(preArr[0]);
if(tn>0&&tn<=126){
IPA++;
if(tn==10){
priIP++;
}
}
else if(tn==127){
errorIP++;
}
else if(tn>=128&&tn<=191){
IPB++;
if(tn==172){
int tn1=Integer.valueOf(preArr[1]);
if(tn1>=16&&tn1<=31){
priIP++;
}
}
}
else if(tn<=223){
IPC++;
if(tn==192){
int tn1=Integer.valueOf(preArr[1]);
if(tn1==168){
priIP++;
}
}
}
else if(tn<=239){
IPD++;
}
else if(tn<=255){
IPE++;
}
else{
errorIP++;
}
}
else{
continue;
}
}
else{
errorIP++;
break;
}
}
}
System.out.println(IPA+" "+IPB+" "+IPC+" "+IPD+" "+IPE+" "+errorIP+" "+priIP);
}
}
}
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<sstream>
int stringtoint(string str){
stringstream ss;
ss<<str;
int x;
ss>>x;
return x;
}
vector<int> toint(string str){
vector<int> res;
string temp;
int ele;
for(int i=0;i<str.length();i++){
if(str[i]!='.'){
temp.push_back(str[i]);
}
else{
ele=stringtoint(temp);
temp.clear();
res.push_back(ele);
}
}
ele=stringtoint(temp);
res.push_back(ele);
return res;
}
bool maskisvalid(vector<int> res){
if(res.size()!=4)
return false;
if(res[0]==255){
if(res[1]==255){
if(res[2]==255){
if(res[3]==254||res[3]==252||res[3]==248||res[3]==240||res[3]==224||res[3]==192||res[3]==128||res[3]==0){
return true;
}
else{
return false;
}
}
else{
if(res[2]==254||res[2]==252||res[2]==248||res[2]==240||res[2]==224||res[2]==192||res[2]==128||res[2]==0){
if(res[3]==0){
return true;
}
else{
return false;
}
}
}
}
else{
if(res[1]==254||res[1]==252||res[1]==248||res[1]==240||res[1]==224||res[1]==192||res[1]==128||res[1]==0){
if(res[3]==0&&res[2]==0){
return true;
}
else{
return false;
}
}
}
}
else{
if(res[0]==254||res[0]==252||res[0]==248||res[0]==240||res[0]==224||res[0]==192||res[0]==128){
if(res[3]==0&&res[2]==0&&res[1]==0){
return true;
}
}
else{
return false;
}
}
return false;
}
int main(){
string str;
int* res=new int[7];
for(int j=0;j<7;j++)
res[j]=0;
while(cin>>str){
string ipstring;
string maskstring;
vector<int> ip,mask;
int i=0;
for(;str[i]!='~';i++){
ipstring.push_back(str[i]);
}
i++;
for(;i<str.length();i++){
maskstring.push_back(str[i]);
}
ip=toint(ipstring);
mask=toint(maskstring);
if(maskisvalid(mask)){
if(ip[1]>=0&&ip[1]<=255&&ip[2]>=0&&ip[2]<=255&&ip[3]>=0&&ip[3]<=255){
if(ip[0]>=1&&ip[0]<=126){
res[0]++;
if(ip[0]==10)
res[6]++;
}
else if(ip[0]>=128&&ip[0]<=191){
res[1]++;
if(ip[0] == 172 && ip[1] >= 16 && ip[1] <= 32)
res[6]++;
}
else if(ip[0]>=192&&ip[0]<=223){
res[2]++;
if(ip[0] == 192 && ip[1] == 168)
res[6]++;
}
else if(ip[0]>=224&&ip[0]<=239){
res[3]++;
}
else if(ip[0]>=240&&ip[0]<=255){
res[4]++;
}
}
}
else{
res[5]++;
}
}
cout<<res[0]<<" "<<res[1]<<" "<<res[2]<<" "<<res[3]<<" "<<res[4]<<" "<<res[5]<<" "<<res[6]<<endl;
return 0;
}